#include<bits/stdc++.h>
using namespace std;
const int N=505,f1[4]={1,0,-1,0},f2[4]={0,1,0,-1};
int n,p,x,y,i,j,d[N][N],ans,w[N][N];
void dfs(int x,int y){
	for(int i=0;i<4;++i){
		int nx=x+f1[i],ny=y+f2[i];
		if(d[nx][ny]>d[x][y]+w[nx][ny])
			d[nx][ny]=d[x][y]+w[nx][ny],dfs(nx,ny);
	}
}
int main(){
	scanf("%d",&n);
	for(i=1;i<=n;++i)for(j=1;j<=n;++j)w[i][j]=1,d[i][j]=min(min(i,n-i+1),min(j,n-j+1));
	for(i=1;i<=n*n;++i){
		scanf("%d",&p);x=(p-1)/n+1;y=(p-1)%n+1;
		ans+=--d[x][y];w[x][y]=0;dfs(x,y);
	}
	printf("%d\n",ans);
}