#include<bits/stdc++.h>
#define Accepted 0
using namespace std;
int n,p[255555],dist[555][555],ans,x,y,lst[555][555];
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
bool In(int x,int y)
{
	return (x>=1 && x<=n && y>=1 && y<=n);
}
void bfs(int x,int y)
{
	queue<pair<int,int> > q;
	q.push(make_pair(x,y));
	while(!q.empty())
	{
		x=q.front().first;y=q.front().second;q.pop();
		for (int i=0;i<4;i++)
		{
			int xx=x+dx[i],yy=y+dy[i];
			if (!In(xx,yy)) continue;
			if (dist[xx][yy]>dist[x][y]+lst[xx][yy])
			{
				dist[xx][yy]=dist[x][y]+lst[xx][yy];
				q.push(make_pair(xx,yy));
			}
		}
	}
}
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n*n;i++) scanf("%d",&p[i]);
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=n;j++)
		{
			dist[i][j]=min(n-j+1,min(j,min(i,n-i+1)));
			lst[i][j]=1;
		}
	}
	for (int i=1;i<=n*n;i++)
	{
		x=(p[i]-1)/n+1;y=(p[i]-1)%n+1;
		ans+=(dist[x][y]-1);
		dist[x][y]--;lst[x][y]=0;
		bfs(x,y);
	}
	printf("%d\n",ans);
	return Accepted;
}