#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr char sp = ' ';
constexpr int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };

int N, P[250000], dep[500][500], res;
bool B[500][500];

int main() {
	cin >> N;
	for (int i = 0; i < N*N; i++) {
		cin >> P[i];
		P[i]--;
	}
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			dep[i][j] = min({ i,j,N - 1 - i,N - 1 - j }) + 1;
			B[i][j] = true;
		}
	}
	for (int i = 0; i < N*N; i++) {
		int X, Y;
		X = P[i] % N;
		Y = P[i] / N;
		res += dep[X][Y] - 1;
		queue<pair<int, int>>que;
		B[X][Y] = false;
		que.push({ X,Y });
		dep[X][Y]--;
		while (!que.empty()) {
			int x = que.front().first;
			int y = que.front().second;
			que.pop();
			for (int i = 0; i < 4; i++) {
				int xx = x + dx[i];
				int yy = y + dy[i];
				if (xx == -1 || yy == -1 || xx == N || yy == N)continue;
				if (B[xx][yy]) {
					if (dep[xx][yy] == dep[x][y] + 2) {
						que.push({ xx,yy });
						dep[xx][yy]--;
					}
				}
				else {
					if (dep[xx][yy] == dep[x][y] + 1) {
						que.push({ xx,yy });
						dep[xx][yy]--;
					}
				}
			}
		}
	}
	cout << res << endl;
}