#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
using namespace std;
const int N=505;
const int dx[]={-1,1,0,0};
const int dy[]={0,0,1,-1};
int n,a[N][N],dis[N][N],vis[N][N],ans;
pii q[N*N];
int main(){
	scanf("%d",&n);
	For(i,1,n) For(j,1,n) dis[i][j]=min(min(i,j),min(n-i+1,n-j+1));
	For(i,1,n*n){
		int p,px,py;
		scanf("%d",&p);
		px=(p-1)/n+1,py=(p-1)%n+1;
		ans+=--dis[px][py]; vis[px][py]=1;
		int h=0,t=0;
		q[++t]=pii(px,py);
		while (h!=t){
			int x=q[++h].fi,y=q[h].se;
			For(d,0,3){
				int nx=x+dx[d],ny=y+dy[d];
				if (nx<1||nx>n||ny<1||ny>n) continue;
				if (dis[x][y]+1-vis[nx][ny]<dis[nx][ny])
					--dis[nx][ny],q[++t]=pii(nx,ny);
			}
		}
	}
	printf("%d\n",ans);
} 