#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N;
vector<vector<int>> A;
vector<vector<bool>> B;
const int di[] = {+1,-1, 0, 0};
const int dj[] = { 0, 0,+1,-1};

void f(int i, int j) {
	for (int k = 0; k < 4; ++ k) {
		int ii = i + di[k];
		int jj = j + dj[k];
		if (0 <= ii && ii < N && 0 <= jj && jj < N) {
			int x = A[i][j] + (B[i][j] ? 0 : 1);
			if (x < A[ii][jj]) {
				A[ii][jj] = x;
				f(ii, jj);
			}
		}
	}
}

int main() {
	cin >> N;

	A.resize(N, vector<int>(N));
	B.resize(N, vector<bool>(N));

	for (int i = 0; i < N; ++ i) for (int j = 0; j < N; ++ j) A[i][j] = min(min(i, N-1-i), min(j, N-1-j));

	int res = 0;
	for (int tt = 0; tt < N*N; ++ tt) {
		int x;
		cin >> x;
		-- x;
		int i = x / N;
		int j = x % N;
		res += A[i][j];
		B[i][j] = true;
		f(i, j);
	}
	cout << res << endl;
}
