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

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template<typename H> bool chmin(H& v1, const H v2) { if (v1 > v2) { v1 = v2; return true; } return false; }
template<typename H> bool chmax(H& v1, const H v2) { if (v1 < v2) { v1 = v2; return true; } return false; }
template<typename H> void read(H& head) { cin >> head; }
template<typename H, typename ...T> void read(H& head, T& ...tail) { cin >> head; read(tail...); }
template<typename H> void write(H head) { cout << head << '\n'; }
template<typename H, typename ...T> void write(H head, T ...tail) { cout << head << " "; write(tail...); }
template<typename ...T> void die(T ...tok) { write(tok...); exit(0); }

int n;
int h[505][505];
bool rm[505][505];

template<class T>
struct Q {
	int h = 0, t = 0;
	T q[250005];
	void reset() { h = t = 0; }
	int size() { return t - h; }
	T front() { // assert(h < t);
		return q[h]; }
	bool empty() { return size() == 0; }
	void pop() { h++; }
	void push(T x) { q[t++] = x; }
};

queue<pii> qq;
const int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);

	read(n);
	
	// calculate initial cost
	rep(i, 1, n + 1) rep(j, 1, n + 1) {
		h[i][j] = min(
				min(i - 1, n - i),
				min(j - 1, n - j));
	}

	ll ans = 0;
	rep(i, 0, n * n) {
		int t; cin >> t; --t;
		qq.reset();
		int p = t / n + 1, q = t % n + 1;
		ans += h[p][q];
		rm[p][q] = 1;
		qq.push({p, q});
		while (!qq.empty()) {
			int x, y; tie(x, y) = qq.front(); qq.pop();
			rep(d, 0, 4) {
				int nx = x + dir[d][0], ny = y + dir[d][1];
				int nd = h[x][y] + int(!rm[x][y]);
				if (nd < h[nx][ny]) {
					h[nx][ny] = nd;
					qq.push({nx, ny});
				}
			}
		}
	}

	die(ans);
}
