#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=505,p=998244353,inf=0x3f3f3f3f;
int read(){
	int f=1,g=0;
	char ch=getchar();
	for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-1;
	for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0';
	return f*g;
}
int n,a[N*N],h[N][N],g[N][N],ans;
void work(int x,int y){
	g[x][y]--;
	if (g[x][y+1]>g[x][y]+h[x][y+1]) work(x,y+1);
	if (g[x][y-1]>g[x][y]+h[x][y-1]) work(x,y-1);
	if (g[x+1][y]>g[x][y]+h[x+1][y]) work(x+1,y);
	if (g[x-1][y]>g[x][y]+h[x-1][y]) work(x-1,y);
}
int main(){
//	freopen("a.in","r",stdin);
//	freopen("a.out","w",stdout);
	n=read();
	for (int i=1;i<=n*n;i++) a[i]=read();
	for (int i=1;i<=n;i++)
	for (int j=1;j<=n;j++)
	h[i][j]=1,g[i][j]=min(min(i,n-i+1),min(j,n-j+1));
	for (int i=1;i<=n*n;i++){
		int x=(a[i]-1)/n+1,y=(a[i]-1)%n+1;
		h[x][y]=0;
		work(x,y);
		ans+=g[x][y];
	}
	printf("%d\n",ans);
	return 0;
}