#include<stdio.h>
using namespace std;

int r[200005], k[200005];
int i,n,pom1,pom2;
long long l,br,q;

int main()
{
	scanf("%d",&n);
	for (i=1;i<=n;i++)
	{
		scanf("%d%d",&pom1,&pom2);
		r[pom1]++; k[pom2]++;
	}
	
	for (i=1;i<=n;i++)
	{
		if (r[i]==0) {br++; l=l+br; continue;}
		if (r[i]==1) {l=l+br; continue;}
		q=r[i]-1;
		if (q<=br) {br=br-q; l=l+br; continue;}
		q=q-br;
		br=0;
		l=l+q;
		r[i+1]=r[i+1]+q; 
	}
	
	br=0; q=0;
	
	for (i=1;i<=n;i++)
	{
		if (k[i]==0) {br++; l=l+br; continue;}
		if (k[i]==1) {l=l+br; continue;}
		q=k[i]-1;
		if (q<=br) {br=br-q; l=l+br; continue;}
		q=q-br;
		br=0;
		l=l+q;
		k[i+1]=k[i+1]+q; 
	}
	
	printf("%lld",l);
}