import sys
from collections import deque

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

n2 = n + 2
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)]

seats = [-1] * n2
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)
q = deque()
popl = q.popleft
appl = q.appendleft
appr = q.append
clrq = q.clear

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

    appr((res, i))
    updated = set()

    while q:
        val, j = popl()
        if j in updated:
            continue
        updated.add(j)
        seats[j] = val
        if is_seated[j]:
            val += 1
            upd_func = appr
        else:
            upd_func = appl
        for dj in MOVE:
            nj = j + dj
            if seats[nj] > val and nj not in updated:
                upd_func((val, nj))
    clrq()
    # print(p, i, res)
    # pg(seats)

print(ans)
