#include<bits/stdc++.h>
#define ll long long 
#define pii pair<int,int>
#define F first
#define S second
#define mp make_pair
#define vi vector<int>
#define pb push_back
using namespace std;
const int N=505;
bool f[N][N];
int n,ans,dist[N][N],cnt;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
inline bool ok(int x,int y){return (x>=1&&y>=1&&x<=n&&y<=n);}
void dfs(int x,int y)
{
	if(!dist[x][y])return;
	dist[x][y]--;
	for(int i=0;i<4;i++)
	{
		int x2=x+dx[i];
		int y2=y+dy[i];
		if(ok(x2,y2)&&dist[x][y]+1+f[x2][y2]==dist[x2][y2])
		{
			dfs(x2,y2);
		}
	}
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			dist[i][j]=min(min(i-1,n-i),min(j-1,n-j))+1,f[i][j]=true;
	for(int i=0;i<n*n;i++)
	{
		cnt++;
		int x;
		cin>>x;
		int a=x/n+1-(x%n==0),b=x%n;
		if(b==0)b=n;
		dfs(a,b);ans+=dist[a][b];
		f[a][b]=false;
	}
	cout<<ans<<"\n";
	return 0;
}