#include <queue>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair

const int MAXN = 500;
const int MAXM = MAXN*MAXN;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

bool a[MAXN + 5][MAXN + 5];
int d[MAXN + 5][MAXN + 5], N;

void update(int x, int y) {
	queue<pii>que; que.push(mp(x, y));
	while( !que.empty() ) {
		pii f = que.front(); que.pop();
		for(int i=0;i<4;i++) {
			pii g = mp(f.fi + dx[i], f.se + dy[i]);
			if( d[f.fi][f.se] + a[f.fi][f.se] < d[g.fi][g.se] )
				d[g.fi][g.se] = d[f.fi][f.se] + a[f.fi][f.se], que.push(g);
		}
	}
}

int P[MAXM + 5], M;
int main() {
	scanf("%d", &N), M = N*N;
	for(int i=1;i<=M;i++)
		scanf("%d", &P[i]);
	for(int i=1;i<=N;i++)
		for(int j=1;j<=N;j++)
			d[i][j] = min(min(i - 1, j - 1), min(N - i, N - j)), a[i][j] = 1;
	
	int ans = 0;
	for(int i=1;i<=M;i++) {
		int x = (P[i] - 1) / N + 1, y = (P[i] - 1) % N + 1;
		ans += d[x][y], a[x][y] = 0, update(x, y);
	}
	printf("%d\n", ans);
}