import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;

void main()
{
    auto N = readln.chomp.to!int;
    auto ss = new int[][](N, N);
    foreach (i; 0..N) foreach (j; 0..N) {
        ss[i][j] = min(i+1, N-i, j+1, N-j);
    }

    auto memo = new int[][](N, N);

    void recalc(int i, int j, int d, int n) {
        foreach (s; [[1,0], [0,1], [-1,0], [0,-1]]) {
            auto ii = i+s[0];
            auto jj = j+s[1];
            if (ii < 0 || ii >= N || jj < 0 || jj >= N || memo[ii][jj] == n) continue;
            memo[ii][jj] = n;
            if (ss[ii][jj] != 0) {
                if (ss[ii][jj] <= d) continue;
                ss[ii][jj] = d;
            }
            recalc(ii, jj, d + (ss[ii][jj] == 0 ? 0 : 1), n);
        }
    }

    int r;
    foreach (m, p; readln.split.to!(int[])) {
        auto i = (p-1)/N;
        auto j = (p-1)%N;
        auto n = m.to!int+1;
        auto d = ss[i][j];
        r += d-1;
        ss[i][j] = 0;
        memo[i][j] = n;
        recalc(i, j, d, n);

    }
    writeln(r);
}