#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
int n,i,u,w,x[N],y[N];

int solve_dimension(int arr[]){
	int c,s,r;
	c=s=r=0;
	for(i=1;i<=n;++i){
		r+=c+s;
		if(arr[i]==0){
			if(c)--c;
			else ++s;
		}
		else{
			c+=arr[i]-1;
			if(s>c)s-=c,c=0;
			else c-=s,s=0;
		}
	}
	return r;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	for(i=0,cin>>n;i<n;++i){
		cin >> u >> w;
		++x[u]; ++y[w];
	}
	cout << solve_dimension(x) + solve_dimension(y);
}