#include<bits/stdc++.h>
using namespace std;int n,ans;struct node{int l,r;}a[20005],maxx;
bool cmp(node x,node y)
{
if(x.l^y.l) return x.l < y.r;
return x.l < y.r;
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> n;
for(int i(1);i <= n;++i) cin >> a[i].l >> a[i].r;
sort(a+1,a+1+n,cmp);
for(int i(1);i <= n;)
{
maxx = a[i];
for(++i;i <= n && a[i].l <= maxx.r;++i) if(a[i].r > maxx.r) maxx.r = a[i].r;
ans += maxx.r-maxx.l;
}
cout << ans;
}