def resolve():
    import numpy as np
    N = int(input())
    P = [int(i) for i in input().split()]
    tab = np.ones((N, N), dtype=np.int)
    ans = 0
    for p in P:
        x, y = divmod(p - 1, N)
        tab[x][y] = 0
        if min(x, y) == 0 or max(x, y) == N - 1:
            continue
        l_sum = sum(tab[x, :y])
        r_sum = sum(tab[x, y + 1:])
        u_sum = sum(tab[:y, x])
        d_sum = sum(tab[y + 1:, x])
        ans += min(l_sum, r_sum, u_sum, d_sum)
    print(ans)


resolve()
