from numba import njit
import numpy as np


def main(N, P):
    lista = [1, -1, N, -N]
    d = np.zeros((N, N), dtype=np.int32)
    d = d.ravel()
    sit = np.ones_like(d)
    que = []
    count = 0
    for i in range(N*N):
        d[i] = min(i % N, (N-1-i) % N, i//N, N-1-i//N)
    for player in P:
        sit[player-1] = 0
        count += d[player-1]
        que.append(player)
        while len(que) > 0:
            p = que.pop()
            for i in lista:
                if p+i < 1 or p + i > N*N:
                    continue
                if d[p-1]+sit[p-1] < d[p+i-1]:
                    d[p+i-1] = d[p-1]+sit[p-1]
                    que.append(p+i)
    print(count)


def cc_export():
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('main', '(i8,i8[:])')(main)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        exit(0)
    #from my_module import main
    N = int(input())
    P = list(map(int, input().split()))
    main(N, P)
