from collections import defaultdict, deque
import sys
input = sys.stdin.buffer.readline

N = int(input())
P_list = list(map(int, input().split()))


def coordinate(P):
    p, q = divmod(P, N)
    if q == 0:
        return p - 1, N - 1
    else:
        return p, q - 1


def around(i, j):
    ans = []
    if i > 0:
        ans.append((i - 1, j))
    if i < N - 1:
        ans.append((i + 1, j))
    if j > 0:
        ans.append((i, j - 1))
    if j < N - 1:
        ans.append((i, j + 1))
    return ans


INF = 10**9
costs = []
for _ in range(N):
    costs.append([INF] * N)
for i in range(N):
    for j in range(N):
        costs[i][j] = min([i - 0, N - 1 - i, j - 0, N - 1 - j])

ans = 0
got_out = defaultdict(bool)

for P in P_list:
    i, j = coordinate(P)
    cost = costs[i][j]
    ans += cost
    got_out[(i, j)] = True
    li = around(i, j)
    # 抜けた席の周辺
    d = deque(li)
    s = {(i, j)}
    while d:
        e = d.popleft()
        if not got_out[e]:
            costs[e[0]][e[1]] = min(costs[e[0]][e[1]], cost)
        else:
            for t in (set(around(e[0], e[1])) - s):
                d.append(t)
        s.add(e)
print(ans)
