#include<bits/stdc++.h>
using namespace std;
const int M=998244353;
const int N=500005,E=524288;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int n,i,p[N],f[N],j,q[N],l,r,d[N],vis[N];
long long fa[N],ans;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int id(int x,int y)
{
	if(x<=0||y<=0||x>n||y>n)
		return n*n+1;
	return (x-1)*n+y;
}
int main()
{
	scanf("%d",&n);
	for(i=1;i<=n*n;++i)
		scanf("%d",&p[i]);
	for(i=1;i<=n;++i)
		for(j=1;j<=n;++j)
		{
			vis[id(i,j)]=min(min(i,n-i+1),min(j,n-j+1))-1;
			d[id(i,j)]=1;
		}
	for(i=1;i<=n*n;++i)
	{
		d[p[i]]=0;
		ans+=vis[p[i]];
		q[l=r=1]=p[i];
		while(l<=r)
		{
			for(j=0;j<4;++j)
			{
				int xx=(q[l]-1)/n+1+dx[j];
				int yy=(q[l]-1)%n+1+dy[j];
				if(vis[id(xx,yy)]>vis[q[l]]+d[q[l]])
				{
					vis[id(xx,yy)]=vis[q[l]]+d[q[l]];
					q[++r]=id(xx,yy);
				}
			}
			++l;
		}
	}
	cout<<ans;
}