#include <bits/stdc++.h>
using namespace std;

const int N = 500 + 10;
const int dx[] = { 1, -1, 0, 0 };
const int dy[] = { 0, 0, 1, -1 };

int n;
int dp[N][N], have[N][N];

bool in (int i, int j) { return 1 <= i && i <= n && 1 <= j && j <= n; }

void upd (int i, int j) {
	for (int k = 0; k < 4; k ++) {
		int x = i + dx[k];
		int y = j + dy[k];
		if (in(x, y) && dp[i][j] + have[i][j] < dp[x][y]) {
			dp[x][y] = dp[i][j] + have[i][j];
			upd(x, y);
		}
	}
}

int main (void) {
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++) {
		for (int j = 1; j <= n; j ++) {
			have[i][j] = 1;
			dp[i][j] = min({ i - 1, j - 1, n - i, n - j });
		}
	}
	long long ans = 0;
	for (int i = 1, x, y; i <= n * n; i ++) {
		scanf("%d", &y);
		x = (y - 1) / n + 1;
		y = (y - 1) % n + 1;
		ans += dp[x][y];
		have[x][y] = 0;
		upd(x, y);
	}
	printf("%lld\n", ans);
}