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

const int n = 500 + 10;

int h[n][n], a[n][n];

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] + a[x][y];
    for (int k = 0; k < 4; k++) {
        int tx = x + dx[k], ty = y + dy[k];
        if (h[tx][ty] > z) {
            h[tx][ty] = z;
            dfs(tx, ty);
        }
    }
}

int main() {
    int n;  scanf("%d", &n);
    
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            h[i][j] = min(min(i - 1, n - i), min(j - 1, n - j));
            a[i][j] = 1;
        }
    }

    int ans = 0;
    for (int i = 0; i < n*n; i++) {
        int pos;    scanf("%d",&pos);
        int x = (pos - 1) / n + 1, y = (pos - 1) % n + 1;
        ans += h[x][y];
        a[x][y] = 0;
        dfs(x, y);
    }
    printf("%d\n", ans);
}
