#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int i,j;
	bool r[n]={false},c[n]={false};
	vector<pair<int,int> > v;
	for(i=0;i<n;i++)
	{
		int a,b;
		cin>>a>>b;
		a--; b--;
		if(r[a]==false && c[b]==false)	
		{
			r[a]=true;
			c[b]=true;	
		}
		else
			v.push_back(make_pair(a,b));
	} 
	vector<pair<int,int> > s;
	int ans=0;
	for(i=0;i<n;i++)
	{                   
		if(r[i]==true)
			continue;	
		for(j=0;j<n;j++)
		{
			//cout<<1<<" ";
			if(c[j]==false && (s.size()==0 || j>s[(int)(s.size()-1)].second))
			{
				s.push_back(make_pair(i,j));
				ans+=abs(v[(int)(s.size())-1].first-i)+abs(v[(int)(s.size())-1].second-j);
				break;
			}
		}                                
	}
	//cout<<v.size();
	cout<<ans;
}