#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1000005;
ll r[maxn], c[maxn], n;
ll zlatnik(ll a[]){
	ll z=0;
	for(ll i=0;i<n-1;i++){
		if(a[i] == 0){
			a[i]++;
			z++;
			a[i+1]--;
		}
		else{
			z=z+abs(a[i]-1);
			a[i+1]=a[i+1]-1+a[i];
			a[i]=1;
		}
	}
	return z;
}
int main()
{
	cin >> n;
	for(int i=0;i<n;i++){
		ll x, y;
		cin >> x >> y;
		x--,y--;
		r[x]++,c[y]++;
	}
	cout << zlatnik(r)+zlatnik(c) << endl;
	return 0;
}