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

typedef long long ll;

const int N = 5e3 + 15, inf = 1e9 + 7;
int n, ans;
int p[N * N];
int val[N][N];
bool mark[N][N];
int dx[] = {-1, 0, 0, +1};
int dy[] = {0, -1, +1, 0};

bool in(int x, int y) {
	return 0 <= x && x < n && 0 <= y && y < n;
}

void dfs(int x, int y, int va) {
	val[x][y] = min(val[x][y], va);
	for (int k = 0; k < 4; k++) {
		int nx = x + dx[k], ny = y + dy[k];
		if (!in(nx, ny))
			continue;
		if (va + !mark[nx][ny] < val[nx][ny])
			dfs(nx, ny, va + !mark[nx][ny]);
	}
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n;
	for (int i = 0; i < n * n; i++)
		cin >> p[i];
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			val[i][j] = min({i, j, n - i - 1, n - j - 1}) + 1;

	for (int i = 0; i < n * n; i++) {
		int x = (p[i] - 1) / n, y = (p[i] - 1) % n;
		ans += val[x][y] - 1;
		mark[x][y] = true;

		int mn = inf;
		for (int k = 0; k < 4; k++) {
			int nx = x + dx[k], ny = y + dy[k];
			if (in(nx, ny))
				mn = min(mn, val[nx][ny]);
			else
				mn = 0;
		}
		dfs(x, y, mn);
	}

	cout << ans << "\n";
	return 0;
}