from collections import deque
import sys
imput = sys.stdin.readline


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

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

occ = [True] * (n * n)
ans = 0
for start in p:
    start -= 1
    q = deque([start])
    ans += cost[start]
    occ[start] = False
    cost[start] -= 1
    while q:
        ij = q.popleft()
        i, j = divmod(ij, n)
        for di, dj in diff:
            nxt_i = i + di
            nxt_j = j + dj
            nxt_ij = nxt_i * n + nxt_j
            if not(0 <= nxt_i < n and 0 <= nxt_j < n):
                continue
            elif not occ[ij] and not occ[nxt_ij] and cost[nxt_ij] > cost[ij]:
                cost[nxt_ij] = cost[ij]
                q.append(nxt_ij)
            elif cost[nxt_ij] > cost[ij] + 1:
                cost[nxt_ij] = cost[ij] + 1
                q.append(nxt_ij)

print(ans)