#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 505;
int s[maxn][maxn];
int cur[maxn][maxn];
int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,1,-1 };
void dfs(int x, int y)
{
	int z = s[x][y] + cur[x][y];
	for (int i = 0; i < 4; i++)
	{
		int x1 = x + dx[i];
		int y1 = y + dy[i];
		if (s[x1][y1] > z)
		{
			s[x1][y1] = z;
			dfs(x1, y1);
		}
	}
}
int main()
{
	int n, ans = 0;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			s[i][j] = min(min(i - 1, n - i), min(j - 1, n - j));
			cur[i][j] = 1;
		}
	}
	for (int j = 1; j <= n * n; j++)
	{
		int p;
		cin >> p;
		int x = (p + n - 1) / n;
		int y = p % n;
		if (y == 0)
			y += n;
		ans += s[x][y];
		cur[x][y] = 0;
		dfs(x, y);
	}
	cout << ans << endl;
	return 0;
}