#include <cstdio>
#include <algorithm>
#include <deque>

#define fi first
#define se second
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int Inf = 0x3f3f3f3f;
const int MN = 505;

int N, A[MN][MN], P[MN * MN];
inline void g(int x, int &i, int &j) {
	i = (x - 1) / N + 1;
	j = (x - 1) % N + 1;
}

std::deque<std::pair<int, int>> q;
int D[MN][MN], Ans;

int main() {
	scanf("%d", &N);
	for (int i = 1; i <= N; ++i)
		for (int j = 1; j <= N; ++j) {
			A[i][j] = 1;
			D[i][j] = std::min({i - 1, N - i, j - 1, N - j});
		}
	for (int i = 1; i <= N * N; ++i) {
		int pos, x, y;
		scanf("%d", &pos);
		g(pos, x, y);
		Ans += D[x][y];
		A[x][y] = 0;
		q.push_back({x, y});
		while (!q.empty()) {
			auto p = q.front(); q.pop_front();
			int ux = p.fi, uy = p.se;
			for (int d = 0; d < 4; ++d) {
				int vx = ux + dx[d], vy = uy + dy[d];
				if (vx < 1 || vx > N || vy < 1 || vy > N) continue;
				if (D[vx][vy] > D[ux][uy] + A[ux][uy]) {
					D[vx][vy] = D[ux][uy] + A[ux][uy];
					A[ux][uy] ? q.push_back({vx, vy}) : q.push_front({vx, vy});
				}
			}
		}
	}
	printf("%d\n", Ans);
	return 0;
}