def main():
    import sys
    import time
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    N = int(input())
    P = [int(a) for a in input().strip().split()]
    G = [[min(n, N-n-1,i, N-i-1) for i in range(N)] for n in range(N)]
    Gflag = [[1] * N for _ in range(N)]
    ans = 0

    vecs = [(1, 0), (0, 1), (-1, 0), (0, -1)]

    def prg():
        for n in range(N):
            print(G[n], Gflag[n])

    def update(row, col, cost):
        G[row][col] = cost
        for vr, vc in vecs:
            nrow, ncol = row + vr, col + vc
            if nrow < 0 or nrow >= N or ncol < 0 or ncol >= N:
                continue
            if G[nrow][ncol] > cost + Gflag[row][col]:
                update(nrow, ncol, cost + Gflag[row][col])

        return None        

    # prg()
    for p in P:
        # print('p = {}'.format(p))
        row, col = (p-1) // N, (p-1) % N
        ans += G[row][col]
        Gflag[row][col] = 0
        update(row, col, G[row][col])
        # prg()
        # print('ans = {}'.format(ans))

    print(ans)
    
if __name__ == '__main__':
    main()