#include<bits/stdc++.h>
using namespace std;
#define N 510
int f[N][N],n,que[N*N][2],c[N][N];
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i)for(int j=1;j<=n;++j){
		f[i][j]=min(min(i,n+1-i),min(j,n+1-j));
		c[i][j]=1;
	}int ans=0;
	for(int i=1;i<=n*n;++i){
		int x,y,P;scanf("%d",&P);
		x=(P-1)/n+1;y=P-(x-1)*n;
		int R=min(min(f[x+1][y],f[x-1][y]),min(f[x][y+1],f[x][y-1]));
		ans=ans+R;
		c[x][y]=0;f[x][y]=R;
		int t=1;
		que[1][0]=x;que[1][1]=y;
		for(int i=1;i<=t;++i){
			x=que[i][0],y=que[i][1];
			if(y&&f[x][y-1]>f[x][y]+c[x][y-1]){
				f[x][y-1]=f[x][y]+c[x][y-1];++t;
				que[t][0]=x;que[t][1]=y-1;
			}
			if(y&&f[x][y+1]>f[x][y]+c[x][y+1]){
				f[x][y+1]=f[x][y]+c[x][y+1];++t;
				que[t][0]=x;que[t][1]=y+1;
			}
			if(y&&f[x-1][y]>f[x][y]+c[x-1][y]){
				f[x-1][y]=f[x][y]+c[x-1][y];++t;
				que[t][0]=x-1;que[t][1]=y;
			}
			if(y&&f[x+1][y]>f[x][y]+c[x+1][y]){
				f[x+1][y]=f[x][y]+c[x+1][y];++t;
				que[t][0]=x+1;que[t][1]=y;
			}
		}
	}printf("%d",ans);
}