#include<bits/stdc++.h>
using namespace std;
const int N = 505;
int x[N*N], y[N*N];
int n;
 
int d[N][N];
bool del[N][N];
const int xx[4]={1,0,-1,0};
const int yy[4]={0,1,0,-1};
 
void extend(int x,int y){
	// cout << x << " " << y << ":" << d[x][y] << endl;
	for(int i=0;i<4;i++){
		int nx=x+xx[i], ny=y+yy[i];
		int nd=d[x][y]+1-del[nx][ny];
		if(d[nx][ny]>nd){
			d[nx][ny]=nd;
			extend(nx,ny);
		}
	}
}
 
int main()
{
	cin >> n;
	for(int i=1;i<=n*n;i++){
		int p;scanf("%d",&p);
		x[i] = (p+n-1)/n,y[i] = p-(x[i]-1)*n;
		// cout << x[i] << ":" << y[i] << endl;
	}
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)d[i][j]=min(min(i,j),min(n-i+1,n-j+1));
	int ans=0;
	for(int i=1;i<=n*n;i++){
		ans+=d[x[i]][y[i]]-1;
		del[x[i]][y[i]]=1;
		for(int j=0;j<4;j++){
			int nx=x[i]+xx[j],ny=y[i]+yy[j];
			d[x[i]][y[i]] = min(d[x[i]][y[i]], d[nx][ny]);
		}
		extend(x[i],y[i]);
		
	}
	cout << ans << endl;
}