#include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define abs(x) ((x)>0?(x):-(x))
#define min(a,b) (a<b?a:b)
#define ll long long
//#define file
using namespace std;

int d[250001][2],f[501][501],a[501][501],n,i,j,k,l,h,t,ans,x,y;
const int fx[2][4]={{-1,0,1,0},{0,1,0,-1}};

int main()
{
	#ifdef file
	freopen("b.in","r",stdin);
	#endif
	
	scanf("%d",&n);
	fo(i,1,n) fo(j,1,n) f[i][j]=min(min(i,n-i+1),min(j,n-j+1)),a[i][j]=1;
	
	fo(i,1,n*n)
	{
		scanf("%d",&k),j=(k-1)/n+1,k=(k-1)%n+1;
		
		--f[j][k];ans+=f[j][k];a[j][k]=0;
		h=0;t=1;
		d[1][0]=j;d[1][1]=k;
		while (h<t)
		{
			++h;
			fo(l,0,3)
			{
				x=d[h][0]+fx[0][l];y=d[h][1]+fx[1][l];
				if (x>=1 && x<=n && y>=1 && y<=n && f[x][y]>f[d[h][0]][d[h][1]]+a[x][y])
				{
					f[x][y]=f[d[h][0]][d[h][1]]+a[x][y];
					++t;d[t][0]=x;d[t][1]=y;
				}
			}
		}
	}
	printf("%d\n",ans);
	
	fclose(stdin);
	fclose(stdout);
	return 0;
}