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

int 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;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= N; j++) {
            C[i][j] = min({i-1, j-1, N-i, N-j});
        }
    }
    auto X = [&](auto x) { return (x - 1) / N + 1; };
    auto Y = [&](auto x) { return (x - 1) % N + 1; };    
    
    long long ans = 0;

    auto bfs = [&](int x, int y) {
        ans += C[x][y];
        U[x][y] = 1;
        queue<tuple<int, int, int>> Q;
        Q.emplace(C[x][y], x, y);
        while (!Q.empty()) {
            auto [z, x, y] = Q.front(); Q.pop();
            for (int i = 0; i < 4; i++) {
                int xt = x + dx[i], yt = y + dy[i];
                if (z+1 == C[xt][yt]) {
                    C[xt][yt]--;
                    if (U[xt][yt]) Q.emplace(C[xt][yt], xt, yt);
                }
            }
        }
    };

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

    cout << ans << "\n";

    return 0;
}