#include <bits/stdc++.h>
using namespace std;
#define R 10000002
int main()
{
int n;
scanf("%d", &n);
pair<int,int> thread[n];
for (int i = 0; i < n; i++) scanf("%d %d", &thread[i].first, &thread[i].second);
sort(thread, thread+n);
int ans = 0;
int from = thread[0].first, to = thread[0].second;
for (int i = 1; i < n; i++)
{
if (thread[i].first <= to)
{
to = max(to, thread[i].second);
}
else
{
ans += to - from;
from = thread[i].first;
to = thread[i].second;
}
}
ans += to - from;
printf("%d", ans);
}