// by Balloons
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mpr make_pair
#define debug() puts("okkkkkkkk")
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)

using namespace std;

typedef long long LL;

const int inf = 1e9;

int n;
int h[505][505], occ[505][505];
const int dx[]={0,0,1,-1};
const int dy[]={1,-1,0,0};

void dfs(int x,int y){
	int curupd = h[x][y] + occ[x][y];
	for(int i=0;i<4;i++){
		int tx = x+dx[i], ty = y+dy[i];
		if(h[tx][ty] > curupd){
			h[tx][ty] = curupd;
			dfs(tx,ty);
		}
	}
}

signed main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			h[i][j] = min(min(i-1,n-i),min(j-1,n-j)),
			occ[i][j] = 1;
	int res=0;
	for(int i=1;i<=n*n;i++){
		int id;scanf("%d",&id);
		int x=(id-1)/n+1,y=id%n == 0?n:id%n;
		res += h[x][y];
		occ[x][y] = 0;
		dfs(x,y);
	}
	printf("%d\n",res);

	return 0;
}

