#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

typedef struct
{
	long long int xp, yp, xv, yv;
	
}Polje;
enum re{Prazan, Pun, Prepun};
void pomeri(re &t)
{
	if(t == Prazan)
		t = Pun;
	if(t == Pun)
		t = Prepun;
}
class redv
{
	public:
		re t;
		int Pre;
		redv()
		{
			t = Prazan;
			Pre = 0;
		}
		bool Check()
		{
			if(t == Prepun)
			{
				Pre++;
				return true;
			}
			else
				pomeri(t);
			return false;
		}
};

unsigned long long int N;
Polje p;

class Red
{
	private:
		vector<redv> x;
		vector<redv> y;
		vector<Polje> v;
		Polje a;
		redv k;
	public:
		Red()
		{
			for(unsigned long long int i = 0; i < N + 1; i++)
			{
				x.push_back(k);
				y.push_back(k);
			}
		}
		void Unos(unsigned long long int xk, unsigned long long int yk)
		{
			a.xp = -1;
			a.xv = -1;
			a.yp = -1;
			a.yv = -1;
			if(x[xk].Check() && y[yk].Check())
			{
				a.xv = xk;
				a.yv = yk;
				v.push_back(a);
			}
		}
		vector<Polje> kraj()
		{
			for(unsigned long long int i = 1; i <= N; i++)
			{
				if(x[i].t == Prazan && y[i].t == Prazan)
				{
					v[i - 1].xp = i;
					v[i - 1].yp = i;
				}
			}
			return v;
		}
};
int main()
{
	cin >> N;
	Red a;
	vector<Polje> v;
	unsigned long long int x, y, pot = 0;
	
	for(unsigned long long int i = 0; i < N; i++)
	{
		cin >> y >> x;
		a.Unos(x, y);
	}
	v = a.kraj();
	for(unsigned long long int i = 0; i < v.size(); i++)
	{
		pot = pot + abs(v[i].xv - v[i].yv) + abs(v[i].xp - v[i].yp);
	}
	cout << pot << endl;
	
	return 0;
}