#include <cstdio>
#include <algorithm>

const int MaxN = 505;

const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};

int n, dp[MaxN][MaxN];
bool vis[MaxN][MaxN];

void dfs(int x, int y) {
	int t = dp[x][y] + !vis[x][y];
	for (int d = 0; d < 4; ++d) {
		int X = x + dx[d];
		int Y = y + dy[d];
		if (X >= 0 && Y >= 0 && X < n && Y < n && dp[X][Y] > t) {
			dp[X][Y] = t;
			dfs(X, Y);
		}
	}
}

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