#include <iostream>
#include <vector>
#include <list>
#include <stdlib.h>
#define ll long long
using namespace std;

vector<ll> redovi;
vector<ll> kolone;

void Unos(int n)
{
	for (int i = 0; i < n; i++)
	{
		redovi.push_back(0);
		kolone.push_back(0);
	}

	ll unos;
	for (int i = 0; i < n; i++)
	{
		cin >> unos;
		redovi[unos - 1]++;
		cin >> unos;
		kolone[unos - 1]++;
	}
}

int IzracunajPoteze(int n, vector<ll> niz)
{
	ll potezi = 0;
	list<ll> nule;

	for (int i = 0; i < n; i++)
	{
		if (niz[i] == 0)
		{
			nule.push_back(i);
		}
	}

	for (int i = 0; i < n; i++)
	{
		if (niz[i] > 1)
		{
			for (ll j = 1; j < niz[i]; j++)
			{
				potezi += abs(i - *(nule.begin()) + 1);
				nule.pop_front();
			}
		}
	}

	return potezi;
}

int main()
{
	int n;
	cin >> n;

	Unos(n);

	ll potezi = 0;
	potezi += IzracunajPoteze(n, redovi);
	potezi += IzracunajPoteze(n, kolone);

	cout << potezi;
	return 0;
}