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

struct S
{
    S* top, bottom, right, left;
    int depth;
}

void main()
{
    auto N = readln.chomp.to!int;
    auto ss = new S[][](N, N);
    foreach (i; 0..N) foreach (j; 0..N) {
        auto s = &ss[i][j];
        if (i) s.top = &ss[i-1][j];
        if (i < N-1) s.bottom = &ss[i+1][j];
        if (j) s.left = &ss[i][j-1];
        if (j < N-1) s.right = &ss[i][j+1];
        s.depth = min(i+1, N-i, j+1, N-j);
    }

    void recalc(S* s, int d) {
        if (s.depth <= d) return;
        s.depth = d;
        if (s.top) recalc(s.top, d+1);
        if (s.bottom) recalc(s.bottom, d+1);
        if (s.left) recalc(s.left, d+1);
        if (s.right) recalc(s.right, d+1);
    }

    int r;
    foreach (p; readln.split.to!(int[])) {
        --p;
        auto s = &ss[p/N][p%N];
        r += s.depth-1;
        if (s.top) s.top.bottom = s.bottom;
        if (s.bottom) s.bottom.top = s.top;
        if (s.left) s.left.right = s.right;
        if (s.right) s.right.left = s.left;

        if (s.top) recalc(s.top, s.depth);
        if (s.bottom) recalc(s.bottom, s.depth);
        if (s.left) recalc(s.left, s.depth);
        if (s.right) recalc(s.right, s.depth);
    }
    writeln(r);
}