#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

int n,p[250001];
int a[250001],z[250001];
vector<int>g[250002];
void cc(int o){
	queue<int>q;
	q.push(o);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		for(int i=0;i<g[u].size();i++)
			if(a[g[u][i]]>a[u]+z[u]){
				a[g[u][i]]=a[u]+z[u];
				q.push(g[u][i]);
			}
	}
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<n;i++)
		for(int j=1;j<=n;j++){
			int u=(i-1)*n+j,v=i*n+j;
			g[u].push_back(v);
			g[v].push_back(u);
		}
	for(int i=1;i<=n;i++)
		for(int j=1;j<n;j++){
			int u=(i-1)*n+j,v=(i-1)*n+j+1;
			g[u].push_back(v);
			g[v].push_back(u);
		}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++){
			a[(i-1)*n+j]=min(min(i-1,n-i),min(j-1,n-j));
			z[(i-1)*n+j]=1;
		}
	n*=n;
	int ans=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&p[i]);
		z[p[i]]=0;
		ans+=a[p[i]];
		cc(p[i]);
	}
	printf("%d\n",ans);
}
