#include<iostream>
#include<algorithm>
using namespace std;
const int N=3e6+10;
#define int long long
struct node{
int x,y;
}a[150005];
int f[N];
bool cmp(node a,node b){
return a.y<b.y;
}
signed main(){
int n;cin>>n;
for(int i=1;i<=n;++i){
cin>>a[i].x>>a[i].y;
}
sort(a+1,a+1+n,cmp);
int now=0;
for(int i=1;i<=n;++i){
int k=a[i].y-a[i].x+1;
if(a[i].x<=now) f[a[i].y]=max(f[now],f[a[i].x-1]+k);
else f[a[i].y]=f[now]+k;
now=a[i].y;
}
cout<<f[a[n].y]<<endl;
return 0;
}