#include<bits/stdc++.h>
#define N 510
using namespace std;
int n,a[N*N],mi[N][N],ex[N][N],ans;
void trans(int x,int y){
	if (x-1>0&&mi[x-1][y]>mi[x][y]+ex[x][y]){mi[x-1][y]--;trans(x-1,y);}
	if (x<n&&mi[x+1][y]>mi[x][y]+ex[x][y]){mi[x+1][y]--;trans(x+1,y);}
	if (y-1>0&&mi[x][y-1]>mi[x][y]+ex[x][y]){mi[x][y-1]--;trans(x,y-1);}
	if (y<n&&mi[x][y+1]>mi[x][y]+ex[x][y]){mi[x][y+1]--;trans(x,y+1);}
}
int main(){
	cin>>n;
	for (int i=1;i<=n*n;i++)cin>>a[i];
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)mi[i][j]=min(i-1,min(j-1,min(n-i,n-j))),ex[i][j]=1;
	for (int i=1;i<=n*n;i++){
		int x=(a[i]+n-1)/n,y=(a[i]-1)%n+1;
		ans+=mi[x][y];
		ex[x][y]=0;
		trans(x,y);
	}
	cout<<ans<<endl;
	return 0;
}