#include <bits/stdc++.h>

using namespace std;

int main(){
	int n;
	cin>>n;
	int rows[n];
	int columns[n];
	int res;
	for(int i =0;i<n;i++){rows[i]=0; columns[i]=0;}
	for(int i =0; i<n;i++){
		int x,y;
		cin>>x>>y;
		
		rows[x-1]++;
		columns[y-1]++;
		
	}
	for(int i =0 ;i<n;i++){
		if(rows[i]>1){
			//search for nearest empty row
			int k = i-1;
			for(int j =i+1;j<n||k>=0;j++){
				//to the right
				if(j<n&&rows[j]==0){
					res+=j-i;
					rows[j]++;
					rows[i]--;
					if(rows[i]==1){break;}
				}
				//to the left
				if(k>=0&&rows[k]==0){
					res+=i-k;
					rows[k]++;
					rows[i]--;
						if(rows[i]==1){break;}
				}
				k--;
			}
		}
		if(columns[i]>1){
			//search for nearest empty column
			int k = i-1;
			for(int j =i+1;j<n||k>=0;j++){
				//to the right
				if(j<n&&columns[j]==0){
					res+=j-i;
					columns[j]++;
					columns[i]--;
					if(columns[i]==1){break;}
				}
				//to the left
				if(k>=0&&columns[k]==0){
					res+=i-k;
					columns[k]++;
					columns[i]--;
					if(columns[i]==1){break;}
				}
				k--;
			}
		}
		
		
	}
	cout<<res;
	return 0;
}