import numpy as np

N = int(input())
a = [int(i) for i in input().split()]

ans = 0
array = np.array([np.arange(N*i+1, N*(i+1)+1)for i in range(N)])
for i in a:
    if i % N == 0:
        array[i//N - 1, -1] = 0
    else:
        row = i//N
        col = i % N - 1
        array[row, col] = 0
        if col == 0:
            continue
        r = array[row]
        rl = np.sum(r[0:col] != 0)
        if rl == 0:
            continue
        rr = np.sum(r[col:] != 0)
        if rr == 0:
            continue
        c = array[:, col]
        ct = np.sum(c[0:row] != 0)
        if ct == 0:
            continue
        cb = np.sum(c[row:] != 0)
        if cb == 0:
            continue
        ans += min(rl, rr, ct, cb)
print(ans)
