using namespace std;
#include<cstdio>
#include<cstring>
#include<algorithm>
#define Nm (1<<17)
#define Inf (1ll<<60)
#define md (l+r>>1)
#define ls (n<<1)
#define rs (n<<1|1)
#define min(a,b) ((a)<(b)?(a):(b))
int X[Nm],n;
long long M[Nm<<1];
struct interval
{
int a,b,c;
} I[Nm];
void read()
{
char S[40];
int i,j;
freopen("stalpi.in","r",stdin);
scanf("%d ",&n);
for(i=0;i<n;++i)
{
gets(S);
for(j=0;S[j]!=' ';++j)
X[i]=X[i]*10+S[j]-'0';
for(++j;S[j]!=' ';++j)
I[i].c=I[i].c*10+S[j]-'0';
for(++j;S[j]!=' ';++j)
I[i].a=I[i].a*10+S[j]-'0';
for(++j;S[j];++j)
I[i].b=I[i].b*10+S[j]-'0';
I[i].a=X[i]-I[i].a;
I[i].b+=X[i];
}
}
bool cmp(interval a, interval b)
{
return a.a<b.a;
}
void build(int n, int l, int r)
{
M[n]=Inf;
if(l<r)
{
build(ls,l,md);
build(rs,md+1,r);
}
}
int p;
long long ans;
void query(int n, int l, int r)
{
ans=min(ans,M[n]);
if(l==r)
return;
if(p<=md)
query(ls,l,md);
else
query(rs,md+1,r);
}
int a,b;
long long v;
void update(int n, int l, int r)
{
if(a<=l && r<=b)
{
M[n]=min(M[n],v);
return;
}
if(a<=md)
update(ls,l,md);
if(b>md)
update(rs,md+1,r);
}
void solve()
{
int i,j=0,step,s;
sort(X,X+n);
sort(I,I+n,cmp);
build(1,0,n-1);
for(step=1;step<=n;step<<=1); step>>=1;
for(i=0;i<n;++i)
{
if(I[j].a>X[i])
continue;
if(i)
{
p=i-1; ans=Inf;
query(1,0,n-1);
}
else
ans=0;
a=i;
for(;I[j].a<=X[i];++j)
{
b=i-1;
for(s=step;s;s>>=1)
if(b+s<n && X[b+s]<=I[j].b)
b+=s;
if(a<=b)
{
v=ans+I[j].c;
update(1,0,n-1);
}
}
}
p=n-1; ans=Inf;
query(1,0,n-1);
}
void write()
{
freopen("stalpi.out","w",stdout);
printf("%lld\n",ans);
}
int main()
{
read();
solve();
write();
return 0;
}