from collections import deque


def to_pos(y, x):
    return y * n + x


def to_coord(pos):
    return pos // n, pos % n


def update(pos):
    que = deque()
    que.append((pos, cost[pos] - 1))
    while que:
        pos, dist = que.popleft()
        if cost[pos] <= dist:
            continue

        y, x = to_coord(pos)
        cost[pos] = dist

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


n = int(input())
p = map(lambda x: int(x) - 1, input().split())

there = [True] * (n * n)
cost = [min(i, n - i - 1, j, n - j - 1) for j in range(n) for i in range(n)]

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

print(ans)
