#include <bits/stdc++.h>
#define pii std::pair<int,int>
#define mp std::make_pair
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
int n,x[250005],y[250005],G[505][505],d[505][505];
int ans = 0;



void bfs(int x,int y){
	std::deque<pii>q;
	q.push_back(mp(x,y));
	while (!q.empty()) {
		pii u = q.front(); q.pop_front();
		for (int dr=0;dr<4;++dr) {
			int x1 = u.first + dx[dr];
			int y1 = u.second + dy[dr];
			if (x1 < 1 || x1 > n || y1 < 1 || y1 > n) continue;
			if (d[x1][y1] > d[u.first][u.second] + G[x1][y1]) {
				d[x1][y1] = d[u.first][u.second] + G[x1][y1];
				if (G[x1][y1]) q.push_back(mp(x1,y1));
				else q.push_front(mp(x1,y1));
			}
		}
	}
}

int main(){
	scanf("%d",&n);
	for (int i=1;i<=n*n;++i) {
		int p;
		scanf("%d",&p);
		x[i] = (p-1)/n + 1;
		y[i] = (p-1)%n + 1;
	}
	for (int i=1;i<=n;++i)
	for (int j=1;j<=n;++j) {
		d[i][j] = std::min( { i, j, n-i+1, n-j+1 } ) ;
		G[i][j] = 1;
	}
	for (int i=1; i <= n*n; i++) {
		d[x[i]][y[i]] --; G[x[i]][y[i]] = 0;
		ans += d[x[i]][y[i]];
		bfs(x[i],y[i]);
	}printf("%d",ans);
	return 0;
}

