import sys
from collections import deque

n, *ppp = map(int, sys.stdin.buffer.read().split())

n2 = n + 2
seats = [-1] * n2
row_base0 = list(range(n))
row_base1 = [-1] + row_base0 + [-1]
row_base0.reverse()
row_base2 = [-1] + row_base0 + [-1]
row_base = [min(a, b) for a, b in zip(row_base1, row_base2)]

for i in range(n):
    ud = min(i, n - 1 - i)
    seats.extend(min(a, ud) for a in row_base)
seats.extend([-1] * n2)

is_seated = [False] * n2
is_seated.extend(([False] + [True] * n + [False]) * n)
is_seated.extend([False] * n2)


def pg(aaa):
    for i in range(0, n2 ** 2, n2):
        print(aaa[i:i + n2])
    print()


ans = 0
MOVE = (-n2, -1, 1, n2)
for p in ppp:
    i, j = divmod(p - 1, n)
    i = (i + 1) * n2 + j + 1
    res = seats[i]
    ans += res
    is_seated[i] = False

    q = deque([(res, i + di) for di in MOVE])
    while q:
        val, j = q.popleft()
        if seats[j] <= val:
            continue
        seats[j] = val
        ist = is_seated[j]
        for dj in MOVE:
            nj = j + dj
            if ist:
                nv = val + 1
                if seats[nj] > nv:
                    q.append((nv, nj))
            else:
                if seats[nj] > val:
                    q.appendleft((val, nj))
    # print(p, i, res)
    # pg(seats)

print(ans)
