#include <bits/stdc++.h>
using namespace std;
						
#define fast_cin() ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
#define int long long

const int N = 505;

int dist[N][N];
bool alive[N][N];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
				
int32_t main() {
	fast_cin();
	int n; cin >> n;  
	for (int r = 0; r < n; ++r) {
		for (int c = 0; c < n; ++c) {
			dist[r][c] = min({r, c, n - 1 - r, n - 1 - c});
		}
	}
	memset(alive, true, sizeof alive);
	int ans = 0;
	for (int turn = 0; turn < n * n; ++turn) {
		int who; cin >> who;
		--who;
		int r = who / n, c = who % n;
		ans += dist[r][c];
		alive[r][c] = 0;
		queue<pair<int, int> > q;
		q.push({r, c});
		while (!q.empty()) {
			auto [i, j] = q.front();
			q.pop();
			for (int d = 0; d < 4; ++d) {
				int ni = i + dx[d], nj = j + dy[d];
				if (ni >= 0 and ni < n and nj >= 0 and nj < n) {
					if (dist[ni][nj] > dist[i][j] + alive[i][j]) {
						dist[ni][nj] = dist[i][j] + alive[i][j];
						q.push({ni, nj});
					}
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}