#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;   
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> 
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define sz(a) (ll)(a.size())
const int M=502;
int dp[M][M],n;
bool vis[M][M];
vector<int> dx = {-1,0,0,1};
vector<int> dy = {0,-1,1,0};
bool check(int p,int q)
{
	if(p<1 || q<1 || p>n || q>1)
		return 0;
	return 1;
}
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	vector<int> vec;
	for(int i=1;i<=n*n;i++)
	{
		int x;
		cin>>x;
		vec.pb(x);
	}
	// sort(all(vec));
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			dp[i][j]=min({i,j,n-i+1,n-j+1});
		}
	}
	// cout<<dp[2][2]<<endl;
	int ans=0;
	for(auto x:vec)
	{
		x--;
		int a=x/n+1,b=x%n+1;
		// cout<<a<<" "<<b<<endl;
		int mn=dp[a][b];
		if(mn)
		ans+=mn-1;
		vis[a][b]=1;
		for(int j=0;j<4;j++)
			mn=min(mn,dp[a+dx[j]][b+dy[j]]);
		dp[a][b]=mn;
		queue<pair<int,int>> q;
		q.push({a,b});
		while(!q.empty())
		{
			auto p = q.front();
			q.pop();
			int x=p.ff,y=p.ss;
			// cout<<x<<" "<<y<<" "<<a<<" "<<b<<endl;
			for(int j=0;j<4;j++)
			{
				int f=0;
				if(vis[x][y]==0)
					f=1;
				if(check(x+dx[j],y+dy[j]))
					continue;
				if(vis[x+dx[j]][y+dy[j]]==0)
					f++;
				if(dp[x+dx[j]][y+dy[j]]>dp[x][y]+f)
				{
					dp[x+dx[j]][y+dy[j]]=dp[x][y]+f;
					q.push({x+dx[j],y+dy[j]});
				}
			}
		}
	}
	cout<<ans<<"\n";
	return 0;
}