#include<bits/stdc++.h>
using namespace std;

const int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};

int d[510][510];
int p[250010],n;
bool bo[510][510];

inline int rd()
{
	int x=0;char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar());
	for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	return x;
}

inline void gao(int x,int y)
{
	for (int i=0;i<4;i++)
	{
		int t1=x+dx[i],t2=y+dy[i];
		if (t1<1||t1>n||t2<1||t2>n) continue;
		if (d[t1][t2]>d[x][y]+bo[t1][t2]) d[t1][t2]=d[x][y]+bo[t1][t2],gao(t1,t2);
	}
}

int main()
{
	n=rd();
	for (int i=1;i<=n*n;i++) p[i]=rd();
	for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) d[i][j]=min(min(i,n-i+1),min(j,n-j+1));
	memset(bo,true,sizeof(bo));
	int ans=0;
	for (int i=1;i<=n*n;i++)
	{
		int x=(p[i]-1)/n+1,y=p[i]-(x-1)*n;
		ans+=d[x][y]-1;bo[x][y]=false;
		d[x][y]--;gao(x,y);
	}
	printf("%d\n",ans);
	return 0;
}