#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
#define int long long
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int N;
int cost[510][510];
int ext[510][510];

void update(int x, int y) {
    queue<P> q;
    q.push(P(x, y));
    while (q.size()) {
        P p = q.front(); q.pop();
        int cx = p.first, cy = p.second;
        rep(i, 4) {
            int nx = cx+dx[i], ny = cy+dy[i];
            if (0<=nx && nx<N && 0<=ny && ny<N && cost[nx][ny]>cost[cx][cy]+ext[cx][cy]) {
                cost[nx][ny] = cost[cx][cy]+ext[cx][cy];
                q.push(P(nx, ny));
            }
        }
    }
}

signed main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> N;
    rep(i, N) rep(j, N) cost[i][j] = min({i, j, N-1-i, N-1-j});
    rep(i, N) rep(j, N) ext[i][j] = 1;
    int ans = 0;
    rep(i, N*N) {
        int P; cin >> P;
        P--;
        ans += cost[P/N][P%N];
        ext[P/N][P%N] = 0;
        update(P/N, P%N);
    }
    cout << ans << endl;
}