#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <random>

using namespace std;

const int dx[] = { -1, 0, +1, 0 };
const int dy[] = { 0, -1, 0, +1 };

int main()
{
	int N;
	cin >> N;
	vector<int> P(N * N);
	for (auto& x : P) {
		cin >> x;
		--x;
	}

	vector<vector<bool>> f(N, vector<bool>(N));
	vector<vector<int>> d(N, vector<int>(N));
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			f[i][j] = true;
			d[i][j] = min({ i, j, N - 1 - i, N - 1 - j });
		}
	}

	long long ans = 0;
	for (int pos : P) {
		int r = pos / N;
		int c = pos % N;

		ans += d[r][c];
		f[r][c] = false;

		queue<int> q;
		q.push(r);
		q.push(c);
		q.push(d[r][c]);
		while (!q.empty()) {
			int y = q.front(); q.pop();
			int x = q.front(); q.pop();
			int c = q.front(); q.pop();
			for (int k = 0; k < 4; ++k) {
				int y2 = y + dy[k];
				int x2 = x + dx[k];
				if (0 <= y2 && y2 < N && 0 <= x2 && x2 < N) {
					int c2 = c + (f[y][x] ? 1 : 0);
					if (d[y2][x2] > c2) {
						d[y2][x2] = c2;
						q.push(y2);
						q.push(x2);
						q.push(c2);
					}
				}
			}
		}
	}
	cout << ans << endl;
}
