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

const int MAXN = 500 + 3;
int H[MAXN][MAXN];
bool occ[MAXN][MAXN];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

void dfs(int x, int y) {
    int z = H[x][y] + occ[x][y];
    for (int t = 0; t < 4; t++) {
        int x1 = x + dx[t], y1 = y + dy[t];
        if (H[x1][y1] > z) {
            H[x1][y1] = z;
            dfs(x1, y1);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N;
    cin >> N;
    
    for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) {
        H[i][j] = min(min(i, N+1-i), min(j, N+1-j)) - 1;
        occ[i][j] = true;
    }

    int res = 0;
    for (int it = 0; it < N*N; it++) {
        int id;
        cin >> id;
        int x = (id+N-1)/N;
        int y = id % N;
        if (y == 0) y += N;
        res += H[x][y];
        occ[x][y] = false;
        dfs(x, y);
    }
    cout << res << "\n";
    
}
