#include <bits/stdc++.h>
using namespace std;
inline void read (int &x) {
	char ch = getchar(); x = 0;
	while (!isdigit(ch)) ch = getchar();
	while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
} const int N = 538;
int n, f[N][N], res, k[N][N];
int dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0};
void update (int x, int y) {
	for (int i = 0; i < 4; ++i) {
		int tx = x + dx[i], ty = y + dy[i];
		if (tx < 1 || ty < 1 || tx > n || ty > n) continue;
		if (f[tx][ty] > f[x][y] + (k[x][y] ^ 1))
			f[tx][ty] = f[x][y] + (k[x][y] ^ 1), update (tx, ty);
	}
}
signed main() {
	read (n); int tmp; 
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= n; ++j) 
			f[i][j] = min (i - 1, min (n - i, min (j - 1, n - j)));
	for (int i = 1, x, y; i <= n * n; ++i) {
		read (tmp); 
		y = tmp % n + n * (tmp % n == 0);
	    x = tmp / n + (y != n); //printf ("%d %d %d\n", x, y, f[x][y]);
		res += f[x][y], k[x][y] = 1; update (x, y);
	} return printf ("%d\n", res), 0;
}