#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N;
	cin >> N;
	vector<vector<int>> cost(N, vector<int>(N, 1));
	int di[4] = {1, 0, -1, 0};
	int dj[4] = {0, 1, 0, -1};
	int res = 0;
	REP(x, N * N) {
		int p;
		cin >> p;
		--p;
		vector<vector<int>> d(N, vector<int>(N, INT_MAX));
		int i = p / N;
		int j = p % N;
		d[i][j] = 0;

		using T = tuple<int, int, int>;
		priority_queue<T, vector<T>, greater<T>> pq;
		pq.emplace(0, i, j);
		int c;
		while (!pq.empty()) {
			tie(c, i, j) = pq.top();
			pq.pop();
			if (c > d[i][j]) {
				continue;
			}
			if ((i == 0 || i == N - 1) || (j == 0 || j == N - 1)) {
				res += d[i][j];
				break;
			}

			REP(a, 4) {
				int k = i + di[a];
				int l = j + dj[a];
				if (k >= 0 && k < N && l >= 0 && l < N) {
					if (c + cost[k][l] < d[k][l]) {
						d[k][l] = c + cost[k][l];
						pq.emplace(d[k][l], k, l);
					}
				}
			}
		}
		cost[p / N][p % N] = 0;
	}
	cout << res << endl;

	return 0;
}
