from collections import deque
n = int(input())
Ps = list(map(lambda x: int(x)-1, input().split()))
cost = [min(min(i, n-1-i), min(j, n-1-j)) for i in range(n) for j in range(n)]
DIRC = [(-1, 0), (0, 1), (1, 0), (0, -1)]
exist = [1]*(n**2)


def to_idx(p):
    i, j = divmod(p, n)
    return i, j


def to_v(i, j):
    return i*n+j


def bfs(init_v):
    next_v = deque([to_idx(init_v)])
    exist[init_v] = 0
    while next_v:
        i, j = next_v.popleft()

        for di, dj in DIRC:
            x, y = i+di, j+dj
            if x < 0 or y < 0 or n <= x or n <= y:
                continue

            if cost[to_v(x, y)] <= cost[to_v(i, j)]+exist[to_v(i, j)]:
                continue
            cost[to_v(x, y)] = cost[to_v(i, j)]+exist[to_v(i, j)]
            next_v.append((x, y))


ans = 0
for p in Ps:
    ans += cost[p]
    bfs(p)
print(ans)