#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll mod=998244353;
ll mul(ll a, ll b)
{
	return ((a%mod)*(b%mod))%mod;
}
ll add(ll a, ll b)
{
	return ((a%mod)+(b%mod))%mod;
}
ll sub(ll a, ll b)
{
	return ((a%mod)-(b%mod)+mod)%mod;
} 
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
ll n;
ll out[503][503];
ll occ[503][503];
void dfs(ll x, ll y)
{
	ll z=out[x][y]+occ[x][y];
	for(ll i=0;i<4;i++)
	{
		ll x1=x+dx[i];
		ll y1=y+dy[i];
		if(out[x1][y1]>z)
		{
			out[x1][y1]=z;
			dfs(x1,y1);
		}
	}
}
int main() 
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	for(ll i=1;i<=n;i++)
	{
		for(ll j=1;j<=n;j++)
		{
			out[i][j]=min(min(i-1,abs(n-i)),min(j-1,abs(n-j)));
			occ[i][j]=1;
	
		}
	}
	ll m=n*n;
	ll pr=0;
	while(m--)
	{
		ll id;
		cin>>id;
		ll x=(id+n-1)/n;
		ll y=id%n;
		if(y==0)
		{
			y=n;
		}
		occ[x][y]=0;
		pr+=out[x][y];
		dfs(x,y);
	}
	cout<<pr;
}