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

bool C[510][510];
bool U[510][510];
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int N; cin >> N;
    int M = N * N;
    vector<int> P(M);
    for (auto &x: P) cin >> x;
    auto X = [&](auto x) { return (x - 1) / N + 1; };
    auto Y = [&](auto x) { return (x - 1) % N + 1; };    
    
    reverse(P.begin(), P.end());

    auto bfs = [&](int x, int y) {
        U[x][y] = 1;
        memset(C, 0, sizeof(C));
        deque<tuple<int, int, int>> Q;
        Q.emplace_back(0, x, y);
        while (!Q.empty()) {
            auto [z, x, y] = Q.front(); Q.pop_front();
            if (x == 0 || y == 0 || x == N+1 || y == N+1) return z;
            for (int i = 0; i < 4; i++) {
                int xt = x + dx[i], yt = y + dy[i];
                if (C[xt][yt]) continue;
                if (U[xt][yt]) {
                    Q.emplace_back(z+1, xt, yt);
                } else {
                    Q.emplace_front(z, xt, yt);
                }
                C[xt][yt] = 1;
            }
        }
    };

    long long ans = 0;
    for (int i = 0; i < M; i++) {
        ans += bfs(X(P[i]), Y(P[i]));
    }

    cout << ans << "\n";

    return 0;
}