#include <bits/stdc++.h>

const int MAX_N = int(5e2) + 10;

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

int n;
int d[MAX_N][MAX_N], vis[MAX_N][MAX_N];

void dfs(int x, int y) {
	for (int k = 0; k < 4; ++k) {
		int _x = x + dx[k], _y = y + dy[k], w = d[x][y] + 1 - vis[_x][_y];
		if (w < d[_x][_y]) {
			d[_x][_y] = w;
			dfs(_x, _y);
		}
	}
}

int x[MAX_N * MAX_N], y[MAX_N * MAX_N];

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