#include<cstdio>
#include<algorithm>
#define N 505
#include<queue>
using namespace std;
int n;
bool b[N*N];
int dis[N*N];
int que[N*N], ql, qr;
void bfs(int x){
	que[ql=qr=1]=x;
	while(ql<=qr){
		int u = que[ql++];
		int i=u/n, j=u%n;
		if(i>0 && dis[u-n]>dis[u]+b[u]) dis[u-n]=dis[u]+b[u], que[++qr] = u-n;
		if(j>0 && dis[u-1]>dis[u]+b[u]) dis[u-1]=dis[u]+b[u], que[++qr] = u-1;
		if(i<n-1 && dis[u+n]>dis[u]+b[u]) dis[u+n]=dis[u]+b[u], que[++qr] = u+n;
		if(j<n-1 && dis[u+1]>dis[u]+b[u]) dis[u+1]=dis[u]+b[u], que[++qr] = u+1;
	}
}
int main(){
	scanf("%d", &n);
	for(int x=0; x<n*n; ++x){
		b[x] = true;
		int i=x/n, j=x%n;
		dis[x] = min(min(i, j), min(n-i-1, n-j-1));
	}
	int ans=0, x;
	for(int r=1; r<=n*n; ++r){
		scanf("%d", &x);
		--x;
		ans+=dis[x];
		b[x] = false;
		//printf("%d ", dis[x]);
		bfs(x);
	}
	printf("%d", ans);
	return 0;
}