#if 0
date +%Y.%m.%d

题解先咕着。
        ▁▃▄▄▄▃▃▃▃▄▶
      ▗▇▀▔    ▔▔▔▔
     ▄▛   ▃▅━━■▄▂
    ▟▊   ▐▘     ▀▙
   ▟▜▌   ▐▖   ▋  ▐▍
  ▟▘ ▜   ▝▀▇▆●▘  ▐▌
▗▟▘   ▜▃       ▁▅▛
▔▀▼▅▄▃▃██▅▄▄▄▅■▀▔   CopyRight :)
      ▔▔▔▔▔▔
#endif
#include <cstdio>
#include <algorithm>
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long ll;

struct {
	inline operator int () { int x; return scanf("%d", &x), x; }
} read;

const int maxn = 505;
int val[maxn][maxn];
int dis[maxn][maxn];

void update (int x, int y, int d) {
	d += val[x][y];
	if (d >= dis[x][y]) return;
	dis[x][y] = d;
	update(x - 1, y, d);
	update(x + 1, y, d);
	update(x, y - 1, d);
	update(x, y + 1, d);
}

int main () {
	int n = read;

	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= n; j ++) {
			val[i][j] = 1;
			dis[i][j] = std::min({i, j, n - i + 1, n - j + 1});
		}

	int ans = 0;
	for (int i = 1; i <= n * n; i ++) {
		int p = read;
		int x = (p - 1) / n + 1, y = (p - 1) % n + 1;
		val[x][y] = 0;
		update(x, y, dis[x][y] - 1);
		ans += dis[x][y];
	}

	printf("%d\n", ans);
}
