#include<bits/stdc++.h>
using namespace std;

#define GO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int

const int maxr = 505;

int n, health[maxr][maxr];
bool done[maxr][maxr];
vector<int> dx = {0, 0, -1, 1};
vector<int> dy = {1, -1, 0, 0};

void update(int x, int y) {

	for (int i = 0; i < 4; i++) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (0 > nx || nx >= n || 0 > ny || ny >= n)
			continue;
		int co = !done[x][y];
		if (health[nx][ny] > health[x][y] + co) {
			health[nx][ny] = health[x][y] + co;
			update(nx, ny);
		}
	}
}

int32_t main()
{
	GO;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			health[i][j] = min(min(i, n - i - 1), min(j, n - j - 1));
			done[i][j] = false;
		}
	}
	int ans = 0;
	for (int i = 0; i < n * n; i++) {
		int p;
		cin >> p;
		int x = (p - 1) / n;
		int y = (p - 1) % n;
		ans += health[x][y];
		done[x][y] = true;
		update(x, y);
	}

	cout << ans << endl;
	return 0;
}

//!