#include<bits/stdc++.h>
using namespace std;
typedef long long lol;
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
int n,f[505][505],px[250005],py[250005],vis[505][505];
lol ans;
void bfs(int x,int y)
{int X,Y,xx,yy;
	queue<int>Q;
	for (int i=0;i<4;i++)
		{
			X=x+dx[i];Y=y+dy[i];
			if (X<=0||X>n||Y<=0||Y>n) continue;
			if (f[X][Y]>f[x][y]+(vis[x][y]==0)) 
			{
				Q.push((X-1)*n+Y);
				f[X][Y]=f[x][y]+(vis[x][y]==0);
			}
		}
	while (!Q.empty())
	{
		int xx=px[Q.front()],yy=py[Q.front()];
		Q.pop();
		for (int i=0;i<4;i++)
		{
			X=xx+dx[i];Y=yy+dy[i];
			if (X<=0||X>n||Y<=0||Y>n) continue;
			if (f[X][Y]>f[xx][yy]+(vis[xx][yy]==0))
			{
				Q.push((X-1)*n+Y);
				f[X][Y]=f[xx][yy]+(vis[xx][yy]==0);
			}
		}
	}
}
int main()
{int i,j,x;
	scanf("%d",&n);
	for (i=1;i<=n;i++)
	{
		for (j=1;j<=n;j++)
			{
				f[i][j]=min(i-1,min(j-1,min(n-i,n-j)));
				px[(i-1)*n+j]=i;py[(i-1)*n+j]=j;
			}
	}
	for (i=1;i<=n*n;i++)
	{
		scanf("%d",&x);
		ans+=f[px[x]][py[x]];
		vis[px[x]][py[x]]=1;
		//printf("%d %d %d\n",px[x],py[x],f[px[x]][py[x]]);
		bfs(px[x],py[x]);
	}
	printf("%lld",ans);
}
