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

int main() {
    int n;
    cin >> n;

    int ds[] = { 1, -1, n, -n };
    vector<int> dist(n * n);
    for (int x = 0; x < n; x++) {
        for (int y = 0; y < n; y++) {
            dist[x * n + y] = min({x, y, n - 1 - x, n - 1 - y});
        }
    }

    vector<int> exists(n * n, 1);
    int ans = 0;
    for (int i = 0; i < n * n; i++) {
        int p;
        cin >> p; --p;
        ans += dist[p];
        exists[p] = 0;
        queue<int> q;
        q.push(p);
        while (!q.empty()) {
            int s = q.front(); q.pop();
            for (int d : ds) {
                int t = s + d;
                if (0 <= t && t < n * n && dist[t] > dist[s] + exists[s]) {
                    dist[t] = dist[s] + exists[s];
                    q.push(t);
                }
            }
        }
    }
    cout << ans << '\n';
}
