#include <stdio.h>
#include <algorithm>
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
class node
{
private:
public:
};
namespace root
{
#define lc(id) (id<<1)
#define rc(id) (id<<1|1)
#define lowbit(id) (id&-id)
#define repeat(times) while(times--)
static const int Buf_size=1<<25,Int_size=25;
static char F[Int_size];
static char _c;
static bool _f;
static int _x,__cnt;
static const signed int base_10=10,zero(48),nine(57),flag_signed(45);
inline int abs(const int &_x)
{
return _x<0?-_x:_x;
}
inline int max(const int &_x,const int &_y)
{
return _x>_y?_x:_y;
}
inline int min(const int &_x,const int &_y)
{
return _x<_y?_x:_y;
}
inline void swap(int &_x,int &_y)
{
static int _z;
_z=_x;
_x=_y;
_y=_z;
return;
}
static char buf[Buf_size],*p1=buf,*p2=buf,obuf[Buf_size],*p3=obuf;
#define digit() (zero<=_c&&_c<=nine)
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,Buf_size,stdin),p1==p2)?EOF:*p1++)
#define putchar(x) (p3-obuf<Buf_size)?(*p3++=x):(fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=x)
template<typename _Tp,const bool is_signed=false>inline void read(_Tp&_x)
{
if(!is_signed)
{
_x=0;
_c=getchar();
while(!digit())_c=getchar();
while(digit())
{
_x=_x*10+(_c^zero);
_c=getchar();
}
return;
}
else
{
_x=0;
_f=false;
_c=getchar();
while(!digit())
{
if(_c==flag_signed)_f=true;
_c=getchar();
}
while(digit())
{
_x=_x*10+(_c^zero);
_c=getchar();
}
if(_f)_x=-_x;
return;
}
}
inline int read()
{
_x=0;
_c=getchar();
while(!digit())
{
_c=getchar();
}
while(digit())
{
_x=_x*10+(_c^zero);
_c=getchar();
}
return _x;
}
template<typename _Tp>inline void write(_Tp _x)
{
if(_x<0)
{
putchar(flag_signed);
_x=-_x;
}
if(_x<base_10)
{
putchar(_x^zero);
return;
}
write(_x/base_10);
putchar(_x%base_10^zero);
}
inline void out(int x,const char end='\n')
{
if(!x)
{
putchar(zero);
putchar(end);
return;
}
if(x<0)
{
x=-x;
putchar(flag_signed);
}
__cnt=0;
while(x)
{
F[++__cnt]=x%base_10;
x/=base_10;
}
while(__cnt)
{
putchar(F[__cnt]^zero);
--__cnt;
}
putchar(end);
return;
}
}
namespace solve
{
using namespace root;
inline void debug()
{
puts("debug here.");
return;
}
inline void finput(char *stream)
{
freopen(stream,"r",stdin);
return;
}
inline void foutput(char *stream)
{
freopen(stream,"w",stdout);
return;
}
long long sum;
class edge
{
public:
long long l,r,length;
inline void input()
{
scanf("%lld %lld",&l,&r);
length=r-l;
}
inline const bool operator<(const edge &other)const
{
return l!=other.l?l<other.l:r<other.r;
}
}e[30005];
int i;
long long f[30005];
inline void main()
{
finput("ex.in");
int n=read();
for(i=1;i<=n;++i)
{
e[i].input();
}
std::sort(e+1,e+n+1);
for(i=1;i<n;++i)
f[i]=1ll<<50;
f[n]=e[n].length;
for(i=n;i;--i)
{
int pos=i+1;
while(pos<=n&&(e[pos].l<=e[i].r))
++pos;
while(pos<=n)
{
f[i]=std::min(f[i],f[pos]+e[i].length);
break;
// ++pos;
}
// printf("%lld %lld",f[i],e[i].length);
}
long long ans=1ll<<60;
for(i=1;i<=n;++i)
{
if(e[i].l<=e[1].r)
ans=std::min(ans,f[i]);
}
printf("%lld\n",ans);
}
}
signed main()
{
solve::main();
return 0;
}
f[i] 表示 n∼i 处理完毕,且 i 为红线的最小价值。