#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200005;
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()
{
	ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
	cin >> n;
	for(ll 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;
}
/*
4
4 1
4 2
4 3
4 4

9
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
3 3
*/