import numpy as np
from collections import deque
from numba import njit


@njit('(i4,i4[::1])', cache=True)
def solve(N, ps):

    sheets = np.ndarray(shape=(N, N), dtype=int)

    for i in range(N):
        rmax = min(i, N-1-i)
        sheets[i] = np.array([min(j, N-1-j, rmax) for j in range(N)])

    total = 0
    q = deque()

    def push(q, x, y, v, passed):
        for px, py in [(x, y-1), (x, y+1), (x-1, y), (x+1, y)]:
            if (px, py) in passed:
                continue
            if px < 0 or N <= px or py < 0 or N <= py:
                continue
            passed.add((px, py))
            q.append((px, py, v))

    for p in ps:
        y, x = divmod(p-1, N)
        v = sheets[x][y]
        total += v
        sheets[x][y] = -1

        passed = {(x, y)}
        push(q, x, y, v, passed)

        while q:
            x, y, v = q.popleft()
            sv = sheets[x][y]
            if sv == -1:
                push(q, x, y, v, passed)
            elif v < sv:
                sheets[x][y] = v
                push(q, x, y, v+1, passed)

    return total


def main():
    N = int(input())
    ps = map(int, input().split())
    print(solve(N, ps))


if __name__ == "__main__":
    main()
