#include<bits/stdc++.h>
#define fo(i,a,b) for((i)=(a);i<=(b);i++)
#define rfo(i,a,b) for((i)=(a);i>=(b);i--)
#define inrange(x,y,z) (((x)>=(y))&&((x)<=(z)))
#define ALL(vec) ((vec).begin(),(vec).end())
#define SOR(vec) sort(ALL(vec))
#define UNI(vec) (vec).erase(unique(ALL(vec)),(vec).end())
using namespace std;
int n,p,x,y,tot;
int dist[505][505],emp[505][505];
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
void upd(int x,int y){
	int nd=dist[x][y]+(!emp[x][y]);
	int nx,ny;
	for(int i=0;i<4;i++){
		nx=x+dx[i],ny=y+dy[i];
		if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&dist[nx][ny]>nd){
			dist[nx][ny]=nd;
			upd(nx,ny);
		}
	}
}
int main(){
	#ifdef FILIN
		#ifndef DavidDesktop
			freopen(FILIN,"r",stdin);
			freopen(FILOUT,"w",stdout);
		#endif
	#endif
	ios::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			dist[i][j]=min(min(i-1,n-i),min(j-1,n-j));
	for(int i=1;i<=n*n;i++){
		cin>>p;
		x=(p-1)/n+1,y=(p-1)%n+1;
		tot+=dist[x][y];
		emp[x][y]=1;
		upd(x,y);
	}
	cout<<tot<<endl;
	return 0;
}
