#include<bits/stdc++.h>
using namespace std;

#define NICE 69

int main(){
	int N; cin >> N;
	vector<vector<int>> mx;
	for(int i = 0; i < N; i++){
		vector<int> t;
		t.resize(N);
		mx.push_back(t);
	}
	for(int i = 0; i < N; i++){
		int a, b; cin >> a >> b;
		a--;
		b--;
		mx[a][b] = 1;
	}
	bool notdone = true;
	vector<int> need;
	int sol = 0;
	while(notdone){
		vector<int> ones;
		for(int i = 0; i < N; i++){
			int sum = 0;
			vector<int> oneposX;
			vector<int> oneposY;
			for(int j = 0; j < N; j++){
				sum += mx[j][i];
				if(mx[j][i] == 1){
					oneposX.push_back(j);
					oneposY.push_back(i);
				}
			}
			ones.push_back(sum);
			if(sum == 0 && i != N-1 && !count(need.begin(), need.end(), i)){
				need.push_back(i);
			}
			if(sum > 1){
				notdone = true;
				bool c = true;
				for(int j = 0; j < oneposX.size() && c; j++){
					bool right = false;
					bool left = false;
					bool r = true;
					int X = oneposX[j];
					int Y = oneposY[j];
					if(i > 0 && mx[X][Y-1] == 0)
						left = true;
					if(i < N - 1 && mx[X][Y+1] == 0)
						right = true;
					if(left && need.size()!=0){
						for(int x = 0; x < need.size(); x+=need.size()){
							mx[X][need[x]] = 1;
							mx[X][Y] = 0;
							sol += i - need[x];
							need.erase(need.begin());
						}
						r = false;
						c = false;
					}
					if(right && r){
						mx[X][Y+1] = 1;
						mx[X][Y] = 0;
						sol++;
						c = false;
					}
					sum--;
					ones.push_back(sum);
					if(sum == 1) c = false;
				}
			}
			sort(ones.begin(), ones.end(), greater<int>());
			if(sum == 1 && ones[0] == 1) {
				notdone = false;
			}
		}
	}
	bool notdone2 = true;
	vector<int> need2;
	while(notdone2){
		vector<int> ones;
		for(int i = 0; i < N; i++){
			int sum = 0;
			vector<int> oneposX;
			vector<int> oneposY;
			for(int j = 0; j < N; j++){
				sum += mx[i][j];
				if(mx[i][j] == 1){
					oneposX.push_back(i);
					oneposY.push_back(j);
				}
			}
			ones.push_back(sum);
			if(sum == 0 && i != N-1 && !count(need2.begin(), need2.end(), i)){
				need2.push_back(i);
			}
			if(sum > 1){
				notdone2 = true;
				bool c = true;
				for(int j = 0; j < oneposX.size() && c; j++){
					bool down = false;
					bool up = false;
					bool r = true;
					int X = oneposX[j];
					int Y = oneposY[j];
					if(i > 0 && mx[X-1][Y] == 0)
						up = true;
					if(i < N - 1 && mx[X+1][Y] == 0)
						down = true;
					if(up && need2.size()!=0){
						for(int x = 0; x < need2.size(); x+=need2.size()){
							mx[need2[x]][Y] = 1;
							mx[X][Y] = 0;
							int add = i - need2[x];
							sol += add;
							need2.erase(need2.begin());
						}
						c = false;
						r = false;
					}
					if(down && r){
						mx[X+1][Y] = 1;
						mx[X][Y] = 0;
						sol++;
						c = false;
					}
					sum--;
					ones.push_back(sum);
					if(sum == 1) c = false;
				}
			}
			sort(ones.begin(), ones.end(), greater<int>());
			if(sum == 1 && ones[0] == 1){
				notdone2 = false;
			}
		}
	}
	cout << sol << endl;
	return 0;
}