using namespace std;
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 510
int n;
bool a[N][N];
int f[N][N];
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};
struct DOT{
	int x,y;
} q[N*N];
void expand(int X,int Y){
	int head=1,tail=1;
	q[1]={X,Y};
	while (head<=tail){
		int x=q[head].x,y=q[head].y;
		++head;
		for (int i=0;i<4;++i){
			int tx=x+dx[i],ty=y+dy[i];
			if (tx<1 || tx>n || ty<1 || ty>n || f[x][y]+a[x][y]>=f[tx][ty])
				continue;
			f[tx][ty]=f[x][y]+a[x][y];
			q[++tail]={tx,ty};
		}
	}
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;++i)
		for (int j=1;j<=n;++j)
			a[i][j]=1,f[i][j]=min(min(i-1,n-i),min(j-1,n-j));
	int ans=0;
	for (int i=1;i<=n*n;++i){
		int p,x,y;
		scanf("%d",&p);
		x=(p-1)/n+1;
		y=p%n;
		y=((y==0)?n:y);
		ans+=f[x][y];
		a[x][y]=0;
		expand(x,y);
	}
	printf("%d\n",ans);	
	return 0;
}