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

int a[555][555], b[555][555];
int main() {
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            a[i][j] = min({i, n - i - 1, j, n - j - 1});
    long r = 0;
    for(int k = 0; k < n * n; k++) {
        int p;
        scanf("%d", &p);
        p--;
        r += a[p / n][p % n];
        b[p / n][p % n]++;
        queue<pair<int, int>> q;
        q.emplace(p / n, p % n);
        while(!q.empty()) {
            auto [i, j] = q.front();
            q.pop();
            if(i && a[i - 1][j] > a[i][j] + 1 - b[i][j])
                a[i - 1][j] = a[i][j] + 1 - b[i][j], q.emplace(i - 1, j);
            if(i + 1 < n && a[i + 1][j] > a[i][j] + 1 - b[i][j])
                a[i + 1][j] = a[i][j] + 1 - b[i][j], q.emplace(i + 1, j);
            if(j && a[i][j - 1] > a[i][j] + 1 - b[i][j])
                a[i][j - 1] = a[i][j] + 1 - b[i][j], q.emplace(i, j - 1);
            if(j + 1 < n && a[i][j + 1] > a[i][j] + 1 - b[i][j])
                a[i][j + 1] = a[i][j] + 1 - b[i][j], q.emplace(i, j + 1);
        }
    }
    cout << r << endl;
    return 0;
}
