#include <iostream>
#include <cstdio>
#include <queue>
#define N 505
#define F first
#define S second
using namespace std;

int n, p, r, c, ans, a[N][N], vt[N][N];
int dr[4] = {1, -1, 0, 0};
int dc[4] = {0, 0, 1, -1};
queue<pair<int,int>> qu;

int main() {
	int i, j, tr, tc, cnt;
	cin >> n;
	for(i=0; i<n; i++) for(j=0; j<n; j++) {
		a[i][j] = min(min(i, n-1-i), min(j, n-1-j));
	}
	for(i=0; i<n*n; i++) {
		scanf("%d", &p);
		p--;
		r = p/n, c = p%n;
		ans += a[r][c];
		vt[r][c] = 1;
		qu.push({r,c});
		while(!qu.empty()) {
			r = qu.front().F; c = qu.front().S; qu.pop();
			cnt = a[r][c];
			if(!vt[r][c]) cnt++;
			for(j=0; j<4; j++) {
				tr = r + dr[j];
				tc = c + dc[j];
				if(tr<0 || tr>=n || tc<0 || tc>=n || a[tr][tc]<=cnt) continue;
				a[tr][tc] = cnt;
				qu.push({tr, tc});
			}
		}
	}
	cout << ans;
	return 0;
}