```cpp
#include<iostream>
#include<vector>
#include<utility>
using namespace std;
vector<pair<int, int>>x;
vector<pair<int, int>>y;
int main() {
long long n,begin,end,sum=0,min,max; cin >> n;
for (int i = 0; i < n; i++) {
cin >> begin >> end;
x.push_back(make_pair(begin, end));
}
for (int i = 0; i < n; i++) {
cin >> begin >> end;
y.push_back(make_pair(begin, end));
}
for (int i = 0; i < n; i++) {
for ( int j=0; j < n; j++) {
if (y[j].first > x[i].second)
break;
if (x[i].first > y[j].second)
continue;
else {
min = x[i].first > y[j].first ? x[i].first : y[j].first;
max = x[i].second < y[j].second ? x[i].second : y[j].second;
sum = sum + max - min;
}
}
}
cout << sum << endl;
}