#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read(){
	ll a=0,b=getchar(),c=1;
	while(!isdigit(b))c=b=='-'?-1:1,b=getchar();
	while(isdigit(b))a=a*10+b-'0',b=getchar();
	return a*c;
}
int n,ans,d[505][505],p[505*505],dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
bool b[505][505];
void gao(int x,int y){
	b[x][y]=false;
	queue<pair<int,int> > q;
	q.push(make_pair(x,y));
	while(q.size()){
		pair<int,int> p=q.front();
		q.pop();
		for(int k=0;k<4;k++){
			int z=d[p.first][p.second]+(b[p.first][p.second]);
			int nx=p.first+dx[k],ny=p.second+dy[k];
			if(nx>0 and nx<=n and ny>0 and ny<=n and d[nx][ny]>z){
				d[nx][ny]=z;
				q.push(make_pair(nx,ny));
			}
		}
	}
}
int main(){
	n=read();
	for(int i=1;i<=n*n;i++)
		p[i]=read();
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++){
			d[i][j]=min(min(i-1,n-i),min(j-1,n-j));
			b[i][j]=true;
		}
	for(int i=1;i<=n*n;i++){
		int x=(p[i]-1)/n+1,y=(p[i]-1)%n+1;
		ans+=d[x][y];
		gao(x,y);
	}
	printf("%d",ans);
	return 0;
}