#include<cstdio>
#include<utility>
#include<iostream>
#define RI register int
#define CI const int&
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef pair <int,int> pi;
const int N=505,dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
int n,p[N*N],a[N][N],dis[N][N]; long long ans; pi nw;
inline pi trs(CI x)
{
	if (x%n) return mp(x/n+1,x%n); return mp(x/n,n);
}
inline void DFS(pi nw)
{
	for (RI i=0;i<4;++i)
	{
		pi to=mp(nw.fi+dx[i],nw.se+dy[i]);
		if (to.fi<1||to.fi>n||to.se<1||to.se>n) continue;
		if (dis[to.fi][to.se]>dis[nw.fi][nw.se]+a[nw.fi][nw.se])
		dis[to.fi][to.se]=dis[nw.fi][nw.se]+a[nw.fi][nw.se],DFS(to);
	}
}
int main()
{
	RI i,j; for (scanf("%d",&n),i=1;i<=n*n;++i) scanf("%d",&p[i]);
	for (i=1;i<=n;++i) for (j=1;j<=n;++j) a[i][j]=1,dis[i][j]=min(min(i-1,j-1),min(n-i,n-j));
	for (i=1;i<=n*n;++i) nw=trs(p[i]),ans+=dis[nw.fi][nw.se],a[nw.fi][nw.se]=0,DFS(nw);
	return printf("%lld",ans),0;
}