#include <bits/stdc++.h>

using namespace std;

typedef double D;
typedef long long int LL;

#define st first
#define nd second
#define PLL pair <LL, LL>
#define PII pair <int, int>

const int N = 507;
const int DX[] = {-1, 0, 0, 1};
const int DY[] = {0, -1, 1, 0};

int n;
bool is[N][N];
int dist[N][N];

inline bool exist(int x, int y){
	return 1 <= x && x <= n && 1 <= y && y <= n;
}

void update(int x, int y){
	queue <PII> Q;
	Q.push({x, y});
	
	while(!Q.empty()){
		auto p = Q.front();
		Q.pop();
		
		for(int t = 0; t < 4; ++t){
			int nx = p.st + DX[t], ny = p.nd + DY[t];
			if(exist(nx, ny) && dist[nx][ny] > dist[p.st][p.nd] + is[nx][ny]){
				dist[nx][ny] = dist[p.st][p.nd] + is[nx][ny];
				Q.push({nx, ny});
			}
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= n; ++j){
			is[i][j] = true;
			dist[i][j] = min(min(i, j), min(n - i, n - j) + 1);
		}
	
	int ans = 0;
	for(int i = 1; i <= n * n; ++i){
		int p;
		scanf("%d", &p);
		
		int x = (p - 1) / n + 1;
		int y = (p - 1) % n + 1;

		is[x][y] = false;
		dist[x][y]--;

		ans += dist[x][y];
		update(x, y);
	}
	
	printf("%d\n", ans);
	return 0;
}