#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 505
inline int read(){
    int x=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-')f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        x=(x<<3)+(x<<1)+c-'0';
        c=getchar();
    }
    return x*f;
}
int a[N][N],n,ans,p[N*N],vis[N][N];
const int d[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
inline bool check(int x,int y){
	return x>=1&&y>=1&&x<=n&&y<=n;
}
void dfs(int x,int y){
	for(int i=0;i<4;++i){
		int nx=x+d[i][0],ny=y+d[i][1];
		if(check(nx,ny)&&a[nx][ny]>a[x][y]+(!vis[x][y])){
			a[nx][ny]=a[x][y]+(!vis[x][y]);
			dfs(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){
			a[i][j]=min(min(n-i,i-1),min(n-j,j-1));
		}
	}
	for(int i=1;i<=n*n;++i){
		int x=(p[i]-1)/n+1,y=(p[i]-1)%n+1;
		ans+=a[x][y];
		vis[x][y]=1;
		for(int k=0;k<4;++k){
			int nx=x+d[k][0],ny=y+d[k][1];
			if(check(nx,ny)&&a[nx][ny]>a[x][y]){
				a[nx][ny]=a[x][y],dfs(nx,ny);
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}
