#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 5e2 + 5;
const int dx[] = {1,0,-1,0};
const int dy[] = {0,1,0,-1};

int n;
int a[MAXN][MAXN], f[MAXN][MAXN];

void dfs(int x,int y)
{
	for(int k=0; k<4; ++k)
	{
		int xx=x+dx[k], yy=y+dy[k];
		if(xx<1||xx>n || yy<1||yy>n) continue;
		if(f[xx][yy] > f[x][y] + a[xx][yy])
			f[xx][yy] = f[x][y] + a[xx][yy],
			dfs(xx,yy);
	}
}

int main(void)
{
	scanf("%d",&n);
	for(int i=1; i<=n; ++i)
		for(int j=1; j<=n; ++j)
			a[i][j] = 1,
			f[i][j] = min(min(i,j), min(n-i+1,n-j+1));
	
	int tot = n*n, ans=0;
	for(int i=1; i<=tot; ++i)
	{
		int p;
		scanf("%d",&p);
		int x = (p-1)/n + 1;
		int y = (p-1)%n + 1;
		
		a[x][y]=0;
		--f[x][y];
		dfs(x,y);
		
		ans += f[x][y];
	}
	printf("%d",ans);
	return 0;
}