#include <climits>
#include <cstdio>
#include <algorithm>
#include <functional>
#include <numeric>
using namespace std;
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
struct Seg {
int l, r, i;
bool operator < ( const Seg &s ) const {
return l < s.l;
}
};
const int N = 1e5;
int n;
Seg seg[N];
int main() {
scanf("%d", &n);
forn(i, n)
scanf("%d%d", &seg[i].l, &seg[i].r), seg[i].i = i;
sort(seg, seg + n); // iterators
int last_r = INT_MIN;
int sum = 0;
forn(i, n)
sum += max(0, seg[i].r - max(last_r, seg[i].l)),
last_r = max(last_r, seg[i].r);
printf("%d\n", sum);
}