#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define floop(i,a,n) for (int i=a;i<n;i++)
#define rfloop(i,a,n) for (int i=n-1;i>=a;i--)
#define mloop(it,x) for(it=x.begin();it!=x.end();it++)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define sz(x) ((ll)(x).size())
#define cases ll t; cin>>t; while(t--)
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
typedef long long ll;
typedef unsigned long long int ull;
typedef vector<ll> vec;
typedef pair<ll,ll> pll;
typedef double db;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const ll mod=1000000007;
ll pm(ll x,ll n) {ll result=1; while(n>0){if(n%2==1) result=((result%mod)*(x%mod))%mod; x=((x%mod)*(x%mod))%mod; n/=2;} return result;}
ll p(ll x,ll n) {ll result=1; while(n>0){if(n%2==1) result=result*x; x=x*x; n/=2;} return result;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll ncr(ll n,ll r) { ll p=1,k=1; if(n-r<r) r=n-r; if(r!=0) {while(r!=0) {p*=n; k*=r; ll temp=gcd(p,k); p/=temp; k/=temp; n--; r--;} } else p=1; return p;}
ll factorial[300000+1];
void fact(ll n) { factorial[0]=1; factorial[1]=1; floop(i,2,n+1) factorial[i] = (i * factorial[i-1]%mod) % mod; }
ll d,coeff1,coeff2; 
void euclid(ll a, ll b) { if(b==0) { d=a; coeff1=1; coeff2=0; } else { euclid(b,a%b); ll temp=coeff1; coeff1=coeff2; coeff2=temp-(a/b)*(coeff2); } }
ll modinv(ll a) { euclid(a,mod); return (coeff1%mod+mod)%mod; }
int prime[1000001]={};
int spf[1000001]={};
void sieve(ll n)
{
	prime[0]=1;
	prime[1]=1;
	for(ll i=2;i*i<=n;i++)
	{
		if(prime[i]==0)
		{
			for(ll j=i*i;j<=n;j+=i)
			{
				prime[j]=1;	
				if(spf[j]==j)
				spf[j]=i;	
			}		
		}
	}
}
/*
vec s(300000,0);
vec si(300000,1);
ll root(ll a)
{
	while(s[a]!=a)
	a=s[a];
	return a;
}
void uni(ll a,ll b)
{
	ll r1=root(a);
	ll r2=root(b);
	if(si[r1]<si[r2])
	{
		s[r1]=s[r2];
		si[r2]+=si[r1];
	}
	else
	{
		s[r2]=s[r1];
		si[r1]+=si[r2];
	}
}*/
set<pll> done;
void bfs(ll a, ll b, map < pll,ll > & vis, map<pll,ll> & dist,map<pll,vector<pll> > &adj)
{
	vis[mp(a,b)]=1;
	queue<pll> wow;
	wow.push(mp(a,b));
	while(!wow.empty())
	{
		ll u=wow.front().fi;
		ll v=wow.front().se;
		wow.pop();
		floop(i,0,sz(adj[mp(u,v)]))
		{
			if(vis[mp(adj[mp(u,v)][i].fi,adj[mp(u,v)][i].se)]==0)
			{
				//cout<<"y";
				if(done.count(mp(adj[mp(u,v)][i].fi,adj[mp(u,v)][i].se))==0)
				dist[mp(adj[mp(u,v)][i].fi,adj[mp(u,v)][i].se)]=min(dist[mp(u,v)]+1,dist[mp(adj[mp(u,v)][i].fi,adj[mp(u,v)][i].se)]);// cout<<dist[mp(adj[mp(u,v)][i].fi,adj[mp(u,v)][i].se)]<<" ";
				else
				dist[mp(adj[mp(u,v)][i].fi,adj[mp(u,v)][i].se)]=min(dist[mp(u,v)],dist[mp(adj[mp(u,v)][i].fi,adj[mp(u,v)][i].se)]);
				vis[mp(adj[mp(u,v)][i].fi,adj[mp(u,v)][i].se)]=1;
			//	sum[adj[t][i]]=sum[t]+c[t][adj[t][i]];
				wow.push(mp(adj[mp(u,v)][i].fi,adj[mp(u,v)][i].se));
			}
		}
	}
}
struct my_comp
{
	bool operator()(pair<ll,pll> p1, pair<ll,pll> p2)
	{
		if(p1.fi!=p2.fi)
		return p1.fi>p2.fi;
		return p1.se.fi<p2.se.fi;
	}
};
int main()
{
	ll n;
	cin>>n;
	vec pe(n*n);
	floop(i,0,n*n)
	cin>>pe[i];
	map<pll,vector<pll> > adj;
	floop(i,1,n*n+1)
	{
		floop(j,1,n*n+1)
		{
			if(i-1>=1)
			adj[mp(i,j)].pb(mp(i-1,j));
			if(i+1<=n)
			adj[mp(i,j)].pb(mp(i+1,j));
			if(j-1>=1)
			adj[mp(i,j)].pb(mp(i,j-1));
			if(j+1<=n)
			adj[mp(i,j)].pb(mp(i,j+1));
		}
	}
	//cout<<"yes";
	map < pll,ll > vismai;
	map<pll,ll> distmai;
	floop(i,1,n+1)
	{
		floop(j,1,n+1)
		{
			vismai[mp(i,j)]=0;
			distmai[mp(i,j)]=1e18;
		}
		//distmai[mp(i,i)]=0;
	}
	/*floop(i,1,n+1)
	{
		floop(j,1,n+1)
		{
			floop(k,0,sz(adj[mp(i,j)]))
			cout<<adj[mp(i,j)][k].fi<<" "<<adj[mp(i,j)][k].se<<" ";
			cout<<"\n";
		}
	}*/
	ll ans=0;
	//cout<<sz(pe)<<" ";
	floop(i,0,sz(pe))
	{
		map < pll,ll > vis=vismai;
		map<pll,ll> dist=distmai;
		ll u=ceil((double)pe[i]/n);
		ll v=pe[i]%n;
		if(v==0)
		v=n;
		dist[mp(u,v)]=0;
		bfs(u,v,vis,dist,adj);
		ll temp=(n+1)/2-1;
		/*if(i==5)
		{
			cout<<u<<" "<<v<<"\n";
			floop(l,1,n+1)
			{
				floop(m,1,n+1)
				cout<<dist[mp(l,m)]<<" ";
				cout<<"\n";
			}
		}*/
		floop(j,1,n+1)
		{
			//cout<<dist[mp(1,j)]<<" ";
			temp=min(temp,dist[mp(1,j)]);
			temp=min(temp,dist[mp(j,1)]);
			temp=min(temp,dist[mp(n,j)]);
			temp=min(temp,dist[mp(j,n)]);
		}
		done.insert(mp(u,v));
		ans+=max(temp,0ll);
		//cout<<ans<<"\n";
	}
	cout<<ans<<"\n";
	return 0;
}