X[500][500],Y[500][500];
d[4]={1,0,-1};
N;
dfs(x,y)
{
	int c=X[x][y]+1-Y[x][y];
	for(int r=0;r<4;r++)
	{
		int z=x+d[r],w=y+d[r^1];
		if(0<=z&&0<=w&&z<N&&w<N&&X[z][w]>c)
		{
			X[z][w]=c;
			dfs(z,w);
		}
	}
}
ans;
main(P)
{
	scanf("%d",&N);
	for(int i=0;i<N;i++)for(int j=0;j<N;j++)X[i][j]=i<j?fmin(i,N-j-1):fmin(j,N-i-1);
	for(;~scanf("%d",&P);)
	{
		int x=~-P/N,y=~-P%N;
		ans+=X[x][y];
		Y[x][y]=1;
		dfs(x,y);
	}
	printf("%d",ans);
}