#include <bits/stdc++.h>

int main()
{
	int N;
	cin >> N;
	vector<vector<int>> v(N);
	vector<int> x(N);
	int half = N / 2;
	for (int i = 0; i <= N/2; ++i)
	{
		
		x[i] = i;
	}
	int j = 0;
	for (int i = N - 1; i >= N / 2; --i)
	{
		x[i] = j;
		++j;
	}
	for (int i = 0; i < N; ++i)
	{
		v[i] = x;
	}
	for (int i = 0; i < N; ++i)
	{
		for (int j = 0; j < N; ++j)
		{
			v[i][j] = min(v[i][j], v[j][i]);
		}
	}
	int ans = 0,total=N*N,a,b,val,copy,ac,bc;
	for (int i = 0; i < total; ++i)
	{
		cin >> j;
		--j;
		a= j / N;
		b = j % N;
		ac = a;
		bc = b;
		ans += v[a][b];
		val = v[a][b];
		if (a > N / 2)
		{
			copy = val;
			while (a > 0)
			{
				--a;

				if (v[a][b] == -1)
				{
					continue;
				}
				v[a][b] = min(v[a][b],copy);
				++copy;
			}
		}
		else
		{
			copy = val;
			while (a <N-1)
			{
				++a;

				if (v[a][b] == -1)
				{
				
					continue;
				}
				v[a][b] = min(v[a][b], copy);
				++copy;
			}
		}
		a = ac;
		b = bc;
		if (b > N / 2)
		{
			copy = val;
			while (b> 0)
			{
				--b;

				if (v[a][b] == -1)
				{
					continue;
				}
				v[a][b] = min(v[a][b], copy);
				++copy;
			}
		}
		else
		{
			copy = val;
			while (b < N-1)
			{
				++b;

				if (v[a][b] == -1)
				{
					continue;
				}
				v[a][b] = min(v[a][b], copy);
				++copy;
			}
		}

		v[ac][bc] = -1;
	}
	cout << ans << endl;

	return 0;
}