#include<stdio.h>
#include<queue>
#include<set>
#include<math.h>
using namespace std;
int main()
{//od najmanjeg ka najvecem
	
	priority_queue<long, vector<long>, greater<long> > elektronx; 
	
		priority_queue<long, vector<long>, greater<long> > elektrony; 
	set <long> xx; 
	set <long> yy; 
	pair<set<long>::iterator, bool> par;
	long counter=0,n,x,y,xc,yc;
	scanf("%ld",&n);
	
	
	
	//1 2 3
	for(long i=0;i<n;i++)
	{
		scanf("%ld %ld",&x,&y);
		par=xx.insert(x);
		
		//ako vec ima
		if(par.second==false)
		{
			elektronx.push(x);
			
		}
		par=yy.insert(y);
		
		//ako vec ima
		if(par.second==false)
		{
			elektrony.push(y);
			
		}
	}
	
		
	
	
		for(long i=1;i<n;i++)
		{
			
			
			if(!xx.count(i))
			
			{
			
				xc=elektronx.top();
				
		
			
				counter=counter+abs(xc+1-i);
		
					elektronx.pop();
					
		
			}
				if(!yy.count(i))
			{
		
				yc=elektrony.top();
				
				counter=counter+abs(yc+1-i);
				elektrony.pop();
			}
			
			
		}
		
		
		
		printf("%ld",counter);
		
		
		
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	return 0;
}