#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main() {

	int n,time=0;
	cin >> n;
	vector<int>Row(n,0);
	vector<int>Col(n,0);
	
	for (int i = 0; i < n; i++) {
		int r, c;
		cin >> r >> c;
		Row[r - 1]++;
		Col[c - 1]++;
		
	}
	for (int i = 0; i < n; i++) {
		if (Row[i] > 1) {
			int k, l;
			k = i - 1;
			l = i + 1;
			for (int j = 0; j <= k; j++) {
				if (Row[j] == 0) {
					Row[j] = 1;
					time += (i - j);
					Row[i]--;
				}
				if (Row[i] == 1) break;
			}
			for (int j = n-1; j >= l; j--) {
				if (Row[j] == 0) {
					Row[j] = 1;
					time += (j - i);
					Row[i]--;
				}
				if (Row[i] == 1) break;
			}
		}
		if (Col[i] > 1) {
			int k, l;
			k = i - 1;
			l = i + 1;
			for (int j = 0; j <= k; j++) {
				if (Col[j] == 0) {
					Col[j] = 1;
					time += (i - j);
					Col[i]--;
				}
				if (Col[i] == 1) break;
			}
			for (int j = n-1; j >=l; j--) {
				if (Col[j] == 0) {
					Col[j] = 1;
					time += (j - i);
					Col[i]--;
				}
				if (Col[i] == 1) break;
			}
		}
	}
	cout << time;

	return 0;
}