from collections import deque


diff = ((0, 1), (0, -1), (1, 0), (-1, 0))
n = int(input())
p = list(map(int, input().split()))

cost = [[0] * n for i in range(n)]
for i in range(n):
    for j in range(n):
        cost[i][j] = min(i, j, n - i - 1, n - j - 1) + 1

occ = [[True] * n for i in range(n)]
ans = 0
for start in p:
    i, j = divmod(start - 1, n)
    q = deque([(i, j)])
    ans += cost[i][j] - 1
    occ[i][j] = False

    while q:
        i, j = q.popleft()
        for di, dj in diff:
            nxt_i = i + di
            nxt_j = j + dj
            if not(0 <= nxt_i < n and 0 <= nxt_j < n):
                continue
            elif not occ[i][j] and not occ[i][j] and cost[nxt_i][nxt_j] > cost[i][j]:
                cost[nxt_i][nxt_j] = cost[i][j]
                q.append((nxt_i, nxt_j))
            elif cost[nxt_i][nxt_j] > cost[i][j] + 1:
                cost[nxt_i][nxt_j] = cost[i][j] + 1
                q.append((nxt_i, nxt_j))

print(ans)