#include<bits/stdc++.h>
using namespace std;
const int N=505,dx[]={1,-1,0,0},dy[]={0,0,1,-1};
int n,a[N][N],dp[N][N],x[N*N],y[N*N],qx[N*N],qy[N*N],v[N][N],ans;
void bfs(int bx,int by)
{
	dp[bx][by]--;
	int b=0,e=1;
	qx[0]=bx,qy[0]=by;
	while(b<e)
	{
		int kx=qx[b],ky=qy[b];
		b++;
		for(int i=0;i<4;i++)
		{
			int x=kx+dx[i],y=ky+dy[i];
			if(x<1||x>n||y<1||y>n||dp[x][y]<=dp[kx][ky]+v[x][y])
				continue;
			dp[x][y]=dp[kx][ky]+v[x][y];
			qx[e]=x,qy[e]=y;
			e++;
		}
	}
}
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++)
			dp[i][j]=min(min(i,n-i+1),min(j,n-j+1)),v[i][j]=1;
	for(int i=1;i<=n*n;i++)
	{
		ans+=dp[x[i]][y[i]]-1;
		v[x[i]][y[i]]=0;
		bfs(x[i],y[i]);
		//printf("%d %d %d\n",x[i],y[i],ans);
	}
	printf("%d\n",ans);
	return 0;
}