from numba import njit
import numpy as np


def cc_export():
    @njit('(i8, i8[:, :], b1[:], i8[:], i8)')
    def update(n, que, there, cost, pos):
        l = 0
        i = 0

        que[l, 0] = pos
        que[l, 1] = cost[pos] - 1
        # que[l] = pos, cost[pos] - 1
        l += 1

        while i < l:
            pos, dist = que[i]
            i += 1

            if cost[pos] <= dist:
                continue

            y, x = pos // n, pos % n
            cost[pos] = dist

            if y >= 1:
                pp = (y - 1) * n + x
                cc = int(there[pp])
                #que.append((pp, dist + cc))
                que[l] = (pp, dist + cc)
                l += 1
            if y <= n - 2:
                pp = (y + 1) * n + x
                cc = int(there[pp])
                #que.append((pp, dist + cc))
                que[l] = (pp, dist + cc)
                l += 1
            if x >= 1:
                pp = y * n + (x - 1)
                cc = int(there[pp])
                #que.append((pp, dist + cc))
                que[l] = (pp, dist + cc)
                l += 1
            if x <= n - 2:
                pp = y * n + (x + 1)
                cc = int(there[pp])
                que[l] = (pp, dist + cc)
                l += 1

    @njit('(i8, i8[:])')
    def main(n, p):
        que = np.zeros((10 * n * n, 2), dtype=np.int64)
        there = np.full(n * n, True)
        cost = np.array([
            min(i, n - i - 1, j, n - j - 1) for j in range(n) for i in range(n)
        ],
                        dtype=np.int64)

        ans = 0
        for pos in p:
            ans += cost[pos]
            there[pos] = False
            update(n, que, there, cost, pos)
        return ans

    from numba.pycc import CC
    cc = CC('solution')
    cc.export('update', '(i8, i8[:, :], b1[:], i8[:], i8)')(update)
    cc.export('main', '(i8, i8[:])')(main)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        sys.exit(0)

    from solution import update, main
    n = int(input())
    p = np.array(list(map(lambda x: int(x) - 1,
                          input().split())),
                 dtype=np.int64)
    print(main(n, p))
