#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
#define maxn 505
#define inf 1e9
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

inline int read()
{
	int x=0,w=1; char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') w=-1; c=getchar();}
	while(c<='9'&&c>='0') {x=(x<<1)+(x<<3)+c-'0'; c=getchar();}
	return w==1?x:-x;
}

int p[maxn*maxn],d[maxn][maxn],ans,n,v[maxn][maxn];
const int kx[4]={0,0,1,-1};
const int ky[4]={1,-1,0,0};
queue <int> qx,qy;

inline void bfs()
{
	while(!qx.empty())
	{
		int x=qx.front(),y=qy.front(); qx.pop(); qy.pop();
		for(int i=0;i<=3;i++)
		{
			int tx=x+kx[i],ty=y+ky[i];
			if(tx<=n&&tx>=1&&ty<=n&&ty>=1)
			if(d[tx][ty]>d[x][y]+(v[x][y]==0))
			{
				d[tx][ty]=d[x][y]+(v[x][y]==0);
				qx.push(tx); qy.push(ty);
			}
		}
	}
}

int main()
{
	n=read(); rep(i,1,n*n) p[i]=read();
	rep(i,1,n) rep(j,1,n) d[i][j]=min(min(i-1,n-i),min(j-1,n-j));
	rep(i,1,n*n)
	{
		int x=(p[i]-1)/n+1,y=p[i]%n; if(y==0) y=n;
		ans+=d[x][y]; qx.push(x); qy.push(y); v[x][y]=1; bfs();
	}
	cout<<ans<<endl;
	return 0;
}