#include <bits/stdc++.h>
using namespace std;
const int N = 505;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
int n, p[N * N], dist[N][N], has[N][N];
void upd(int x, int y) {
	int nd = dist[x][y] + has[x][y];
	for (int k = 0; k < 4; k++) {
		int nx = x + dx[k], ny = y + dy[k];
		if (nx >= 1 && ny >= 1 && nx <= n && ny <= n && dist[nx][ny] > nd) {
			dist[nx][ny] = nd;
			upd(nx, ny);
		}
	}
}
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++) {
			dist[i][j] = min(min(i, n - i + 1), min(j, n - j + 1)) - 1;
			has[i][j] = 1;
		}
	}
	int ans = 0;
	for (int i = 1; i <= n * n; i++) {
		int x = (p[i] - 1) / n + 1;
		int y = (p[i] - 1) % n + 1;
		ans += dist[x][y];
		has[x][y] = 0;
		upd(x, y);
	}
	printf("%d\n", ans);
	return 0;
}