#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int n,t,maxx;
int a[N],b[N];
int main()
{
cin>>n;
for(int i=1;i<=n;i++){
int op,ed;
cin>>op>>ed;
maxx=max(maxx,ed);
for(int j=op;j<ed;j++)
a[j]=1;
}
for(int i=1;i<=n;i++){
int op,ed;
cin>>op>>ed;
maxx=max(maxx,ed);
for(int j=op;j<ed;j++)
b[j]=1;
}
for(int i=1;i<=maxx;i++){
if(a[i]==b[i]&&a[i]==1)t++;
}
cout<<t;
}