#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define MAXN 100100
#define ll long long
ll n, m, a[510][510], dis[510][510], p[500100], ans;
void dfs(ll x, ll y)
{
	if(dis[x-1][y] > dis[x][y] + a[x][y])
	{
		dis[x-1][y] = dis[x][y] + a[x][y];
		dfs(x-1, y);
	}
	if(dis[x][y-1] > dis[x][y] + a[x][y])
	{
		dis[x][y-1] = dis[x][y] + a[x][y];
		dfs(x, y-1);
	}
	if(dis[x+1][y] > dis[x][y] + a[x][y])
	{
		dis[x+1][y] = dis[x][y] + a[x][y];
		dfs(x+1, y);
	}
	if(dis[x][y+1] > dis[x][y] + a[x][y])
	{
		dis[x][y+1] = dis[x][y] + a[x][y];
		dfs(x, y+1);
	}
}
int main()
{
	scanf("%lld",&n);
	for(ll i=1;i<=n*n;i++) scanf("%lld",&p[i]);
	for(ll i=1;i<=n;i++)
	{
		for(ll j=1;j<=n;j++)
		{
			a[i][j] = 1;
			dis[i][j] = min(i-1, min(j-1, min(n-i, n-j)));
		}
	}
	int x, y;
	for(ll i=1;i<=n*n;i++)
	{
		y = p[i]%n;
		if(y==0) y=n;
		x = (p[i]-y)/n+1;
		ans += dis[x][y];
		a[x][y] = 0;
		dfs(x, y);
	}
	printf("%lld\n",ans);
	
	return 0;
}