#include <iostream>
#include <vector>
using namespace std;

int redovi[200000];
int kolone[200000];
int nule_redovi[200000];
int nule_kolone[200000];

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int red, kolona;
		cin >> red >> kolona;
		redovi[red]++;
		kolone[kolona]++;
	}

	int j = 1;
	int k = 1;
	for (int i = 1; i <= n; i++)
	{
		if (redovi[i] == 0)
			nule_redovi[j++] = i;
		if (kolone[i] == 0)
			nule_kolone[k++] = i;
	}
	
	long long vreme = 0;
	j = 1;
	k = 1;
	for (int i = 1; i <= n; i++)
	{
		while (redovi[i] > 1)
		{
			vreme += abs(nule_redovi[j++] - i);
			redovi[i]--;
		}
		while (kolone[i] > 1)
		{
			vreme += abs(nule_kolone[k++] - i);
			kolone[i]--;
		}
	}

	cout << vreme << "\n";
	return 0;
}