#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;

int N, A[1 << 18];
int dp[509][509];
bool used[509][509];

void update(int vx, int vy) {
	int dx[4] = { -1, 0, 1, 0 };
	int dy[4] = { 0, 1, 0, -1 };
	
	queue<pair<int, int>> Q; Q.push(make_pair(vx, vy));
	while (!Q.empty()) {
		int px = Q.front().first;
		int py = Q.front().second;
		Q.pop();
		for (int i = 0; i < 4; i++) {
			int sx = px + dx[i], sy = py + dy[i];
			if (sx <= 0 || sy <= 0 || sx > N || sy > N) continue;
			int cost = dp[px][py] + (int)used[px][py];
			if (dp[sx][sy] <= cost) continue;
			dp[sx][sy] = cost;
			Q.push(make_pair(sx, sy));
		}
	}
}

int main() {
	cin >> N;
	for (int i = 1; i <= N * N; i++) cin >> A[i];
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
			dp[i][j] = min({ i - 1,j - 1,N - i,N - j });
			used[i][j] = true;
		}
	}

	long long sum = 0;
	for (int i = 1; i <= N * N; i++) {
		int rx = ((A[i] - 1) / N) + 1, ry = ((A[i] - 1) % N) + 1;
		used[rx][ry] = false;
		sum += dp[rx][ry];
		update(rx, ry);
	}
	cout << sum << endl;
	return 0;
}