#include <bits/stdc++.h>

using namespace std;

const int maxn = 510;

int ans = 0, n;

int dis[maxn][maxn], oc[maxn][maxn];
int p[maxn * maxn];

void upd(int x, int y) {
	if (x - 1 >= 1 && dis[x-1][y] > oc[x-1][y] + dis[x][y]) {
		dis[x-1][y] = oc[x-1][y] + dis[x][y];
		upd(x-1, y);
	}
	if (x + 1 <= n && dis[x+1][y] > oc[x+1][y] + dis[x][y]) {
		dis[x+1][y] = oc[x+1][y] + dis[x][y];
		upd(x+1, y);
	}
	if (y - 1 >= 1 && dis[x][y-1] > oc[x][y-1] + dis[x][y]) {
		dis[x][y-1] = oc[x][y-1] + dis[x][y];
		upd(x, y-1);
	}
	if (y + 1 <= n && dis[x][y+1] > oc[x][y+1] + dis[x][y]) {
		dis[x][y+1] = oc[x][y+1] + dis[x][y];
		upd(x, y+1);
	}
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n * n; i++) scanf("%d", &p[i]);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			oc[i][j] = 1;
			dis[i][j] = min(min(i, j), min(n-i+1, n-j+1));
		}
	}
	for (int i = 1; i <= n*n; i++) {
		int x = (p[i] - 1) / n + 1;
		int y = (p[i] - 1) % n + 1;
		oc[x][y] = 0, -- dis[x][y];
		upd(x, y);
		ans += dis[x][y];
	}
	printf("%d\n", ans);
	return 0;
}