#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
ll mod = 1000000007;

int main() {
source:
    ll n;
    cin >> n;
    ll p[n * n];
    rep(i, n * n) {
        cin >> p[i];
        p[i]--;
    }

    ll distance[n][n] = {};
    rep(i, n) {
        rep(j, n) {
            ll d = min(min(i, n - i - 1), min(j, n - j - 1));
            distance[i][j] = d + 1;
        }
    }

    ll x_ar[] = {-1, 0, 0, 1}, y_ar[] = {0, -1, 1, 0};
    int remain[n][n];
    rep(i, n) {
        rep(j, n) { remain[i][j] = 1; }
    }

    ll ans = 0;
    rep(i, n * n) {
        ll num = p[i];
        ll x = num / n, y = num % n;
        ans += distance[x][y] - 1;
        remain[x][y] = 0;
        queue<pair<ll, ll>> q;
        q.push(make_pair(x, y));
        bool seen[n][n] = {};
        while (!q.empty()) {
            auto p = q.front();
            q.pop();
            ll x_now = p.first, y_now = p.second;
            if (seen[x_now][y_now]) {
                continue;
            }
            seen[x_now][y_now] = true;
            ll value = distance[x_now][y_now];
            rep(j, 4) {
                ll x_next = x_now + x_ar[j], y_next = y_now + y_ar[j];
                if (x_next < 0 || n <= x_next) {
                    continue;
                }
                if (y_next < 0 || n <= y_next) {
                    continue;
                }
                if (distance[x_next][y_next] ==
                    value + remain[x_next][y_next]) {
                    q.push(make_pair(x_next, y_next));
                }
            }
            distance[x_now][y_now]--;
        }
    }

    cout << ans << endl;

    return 0;
}
