#include<bits/stdc++.h>
using namespace std;

const int maxn=505;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0}; 
int n,ql,qr,ans;
int dist[maxn][maxn];
bool occ[maxn][maxn];
pair<int,int>que[maxn*maxn];

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
		dist[i][j]=min(min(i,j),min(n-i+1,n-j+1)),occ[i][j]=true;
	for(int i=1;i<=n*n;i++){
		int p;scanf("%d",&p);
		int x=(p-1)/n+1,y=(p-1)%n+1;
		occ[x][y]=false;
		dist[x][y]--;
		ans+=dist[x][y];
		ql=1;qr=0;
		que[++qr]=make_pair(x,y);
		while(ql<=qr){
			x=que[ql].first;y=que[ql].second;
			ql++;
			for(int d=0;d<4;d++){
				int nx=x+dx[d],ny=y+dy[d];
				if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&dist[nx][ny]>dist[x][y]+occ[nx][ny]){
					dist[nx][ny]=dist[x][y]+occ[nx][ny];
					que[++qr]=make_pair(nx,ny);
				}
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}