#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,i,j,ans,x,y,w,tx,ty,v[502][502],b[502][502],a[1000010],aa[4],bb[4];
queue<int>q;
int main(){
	aa[0]=1;aa[1]=-1;
	bb[2]=1;bb[3]=-1;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	 for(j=1;j<=n;j++)v[i][j]=1,b[i][j]=min(min(i,j),min(n-i+1,n-j+1));
	for(i=1;i<=n*n;i++){
		scanf("%d",&a[i]);
		x=(a[i]-1)/n;y=a[i]-x*n;x++;
		ans+=--b[x][y];v[x][y]=0;
		q.push(a[i]);
		while(!q.empty()){
			w=q.front();q.pop();
			x=(w-1)/n;y=w-x*n;x++;
			for(j=0;j<4;j++){
				tx=x+aa[j];ty=y+bb[j];
				if(!tx||!ty||tx>n||ty>n)continue;
				if(b[x][y]+v[tx][ty]<b[tx][ty]){
					b[tx][ty]=b[x][y]+v[tx][ty];
					q.push((tx-1)*n+ty);
				}
			}
		}
	}
	printf("%d",ans);
}