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

vector<int> redovi;
vector<int> kolone;

void Unos(int n)
{
	for(int i = 0; i < n; i++)
	{
		redovi.push_back(0);
		kolone.push_back(0);
	}
	
	int unos;
	for(int i = 0; i < n; i++)
	{
		cin << unos;
		redovi[unos]++;
		cin >> unos;
		kolone[unos]++;		
	}
}

int IzracunajPoteze(int n, vector<int> niz)
{
	int potezi = 0;
	list<int> 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(int i = 1; i < niz[i]; i++)
			{
				potezi += abs(niz[i] - *nule.begin);
				nule.pop_front();
			}
		}		
	}

	return potezi;
}

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

	int potezi = 0;
	potezi += IzracunajPoteze(int n, redovdi);
	potezi += IzracunajPoteze(int n, kolone);

	cout << potezi;
	return 0;
}