#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>

using namespace std;

const int N = 505;
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 }; 

int d[N * N], a[N * N], q[N * N], n;
bool v[N * N]; int ans = 0;

inline bool pd(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= n; }

void upd(int id) {
	ans += d[id];
	int l = 1, r = 0; q[++r] = id; v[id] = 0;
	for (int nw = q[l]; l <= r; nw = q[++l]) {
		int x = (nw - 1) / n, y = nw - x * n; ++x;
		for (int i = 0; i < 4; ++i) {
			int nx = x + dx[i], ny = y + dy[i];
			if (!pd(nx, ny)) continue;
			int nxt = (nx - 1) * n + ny;
			int cs = 1; if (!v[nw]) cs = 0;
			if (d[nxt] > d[nw] + cs) {
				d[nxt] = d[nw] + cs; 
				q[++r] = nxt;
			}
		}
	}
}
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n * n; ++i) scanf("%d", &a[i]), v[i] = 1;
	for (int i = 1; i <= n * n; ++i) {
		int x = (i - 1) / n, y = i - x * n; ++x;
 		d[i] = min(x - 1, min(y - 1, min(n - x, n - y)));
	}
	for (int i = 1; i <= n * n; ++i) upd(a[i]);
	
	for (int i = 1; i <= n * n; ++i) ans += d[i];
	printf("%d\n", ans);
	return 0;
}