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

const int MAXN = 500 + 3;
int H[MAXN][MAXN];
bool occupy[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] + occupy[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() {
    int N;
    cin >> N;

    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= N; j++) {
            H[i][j] = min({i, N + 1 - i, j, N + 1 - j}) - 1;
            occupy[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];
        occupy[x][y] = false;
        dfs(x, y);
    }
    cout << res << "\n";
}
