#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;

typedef pair <int, int> pii;

int a[510][510];
int dp[510][510];
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};
int P[250010];
queue <pii> q;

int main () {
	int n; scanf("%d", &n);
	for (int i = 0; i < n * n; i++) {
		scanf("%d", &P[i]);
		P[i]--;
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			a[i][j] = 1;
			dp[i][j] = min(min(i + 1, n - i), min(j + 1, n - j));
		}
	}
	int ans = 0;
	for (int i = 0; i < n * n; i++) {
		int x = P[i] / n, y = P[i] % n;
		a[x][y] = 0;
		dp[x][y]--;
		ans += dp[x][y];
		q.push(mp(x, y));
		while (!q.empty()) {
			pii t = q.front(); q.pop();
			int x = t.fi, y = t.se;
			for (int i = 0; i < 4; i++) {
				int xx = x + dir[i][0], yy = y + dir[i][1];
				if (xx < 0 || yy < 0 || xx >= n || yy >= n) continue;
				int ndp = dp[x][y] + a[xx][yy];
				if (ndp < dp[xx][yy]) {
					dp[xx][yy] = ndp;
					q.push(mp(xx, yy));
				}
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}
