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

int n;
int cost[500][500];
int idx[500*500 + 2], idy[500*500 + 2];

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

bool valid(int gg) {
	return gg >= 0 && gg < n;
}

int has[500][500];
int vis[500][500];

void dfs(int x, int y, int cx) {
	vis[x][y] = cx;
	for (int i = 0; i < 4; i++) {
		int nx = x + dx[i];
		int ny = y + dy[i];

		if (valid(nx) && valid(ny) && vis[nx][ny] != cx) {
			if (cost[nx][ny] > cost[x][y] + has[x][y]) {
				cost[nx][ny] = cost[x][y] + has[x][y];
				dfs(nx, ny, cx);
			}
		}
	}
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);	

	cin >> n;
	int petr = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			petr++;
			idx[petr] = i;
			idy[petr] = j;

			has[i][j] = 1;
			cost[i][j] = min(i, n - i - 1);
			cost[i][j] = min(cost[i][j], min(j, n - j - 1));
		}
	}

	int ans = 0;
	for (int ppp = 1; ppp <= n*n; ppp++) {
		int p; cin >> p;
		int x = idx[p], y = idy[p];

		has[x][y] = 0;
		ans += cost[x][y];
		dfs(x, y, ppp);
	}
	cout << ans << '\n';

	return 0;
}
