#include <iostream>

using namespace std;

int n;
int r[555][555];
int z[555][555];
int p[555*555];

void dfs(int y, int x, int c, int f) {
	if (y < 1 || x < 1 || y > n || x > n) return;
	if (f && r[y][x] <= c) return;
	r[y][x] = c;
	dfs(y-1,x,r[y][x]+z[y][x],1);
	dfs(y+1,x,r[y][x]+z[y][x],1);
	dfs(y,x-1,r[y][x]+z[y][x],1);
	dfs(y,x+1,r[y][x]+z[y][x],1);
}

int main() {
	cin >> n;
	for (int i = 1; i <= n*n; i++) cin >> p[i];
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			int m1 = min(i-1,n-i);
			int m2 = min(j-1,n-j);
			r[i][j] = min(m1,m2);
			z[i][j] = 1;
		}
	}
	int t = 0;
	for (int i = 1; i <= n*n; i++) {
		int y = (p[i]-1)/n+1;
		int x = (p[i]-1)%n+1;
		t += r[y][x];
		z[y][x] = 0;
		dfs(y,x,r[y][x],0);
	}
	cout << t << "\n";
}
