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

int free_cell(vector<int> rows, int r, int n)
{
	int min = n + 1;
	for (int i = r - 1; i >= 0; i--)
	{
		if (rows[i] == 0)
		{
			min = i - r;
			break;
		}
	}
	if (abs(min) == 1)
		return r - 1;
	else
	{
		for (int i = r + 1; i < n; i++)
		{
			if (rows[i] == 0 && i - r < abs(min))
			{
				min = i - r;;
				break;
			}
		}
		return r + min;
	}
}

int main()
{
	int n;
	int t = 0;
	cin >> n;
	vector <vector<int>> matrix(n, vector<int>(n, 0));
	vector <int> rows(n, 0);
	vector <int> col(n, 0);
	for (int i = 0; i < n; i++)
	{
		int x, y;
		cin >> x >> y;
		rows[x - 1]++;
		col[y - 1]++;
		matrix[x - 1][y - 1]++;
	}
	for (int i = 0; i < n; i++)
	{
		if (rows[i] > 1)
		{
			for (int j = 0; j < n; j++)
			{
				if (matrix[i][j] == 1 && rows[i] > 1)
				{
					int z = free_cell(rows, i, n);
					matrix[i][j] = 0;
					matrix[z][j] = 1;
					rows[i]--;
					rows[z]++;
					t = t + abs(i - z);
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		if (col[i] > 1)
		{
			for (int j = 0; j < n; j++)
			{
				if (matrix[j][i] == 1 && col[i] > 1)
				{
					int z = free_cell(col, i, n);
					matrix[i][j] = 0;
					matrix[i][z] = 1;
					col[i]--;
					col[z]++;
					t = t + abs(i - z);
				}
			}
		}
	}
	cout << t;
}