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

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

int main() {
	int N;
	cin >> N;
	vector<vector<int> > Dist(N, vector<int>(N, 0));
	vector<vector<bool> > Use(N, vector<bool>(N, true));
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			Dist[i][j] = min({ i, j, N - 1 - i, N - 1 - j });
		}
	}
	int ANS = 0;
	for (int i = 0; i < N * N; i++) {
		int P;
		cin >> P;
		P--;
		int X = P / N, Y = P % N;
		ANS += Dist[X][Y];
		Use[X][Y] = false;
		queue<int> Q;
		Q.push(P);
		while (!Q.empty()) {
			int A = Q.front() / N, B = Q.front() % N;
			Q.pop();
			for (int j = 0; j < 4; j++) {
				int NX = A + dx[j], NY = B + dy[j];
				if (0 <= NX && NX < N && 0 <= NY && NY < N) {
					if (Dist[NX][NY] > Dist[A][B] + Use[A][B]) {
						Dist[NX][NY] = Dist[A][B] + Use[A][B];
						Q.push(NX * N + NY);
					}
				}
			}
		}
	}
	cout << ANS << endl;
}