from collections import deque


DIFFS = [(-1, 0), (0, -1), (0, 1), (1, 0)]


n = int(input())
orders = list(map(int, input().split()))

if n == 2:
    print(0)
    exit(0)

rems = [[True] * n for _ in range(n)]

seats = [[1] * n for _ in range(n)]
for i in range(1, n // 2 + 1):
    for j in range(i, n - i):
        seats[i][j] = seats[n - 1 - i][j] = seats[j][i] = seats[j][n - 1 - i] = i + 1

ans = 0
for order in orders:
    row, col = divmod(order - 1, n)
    ans += seats[row][col] - 1

    rems[row][col] = False

    if row == 0 or row == n - 1 or col == 0 or col == n - 1:
        seats[row][col] = 0
    else:
        min_cost = 1 << 64
        for row_diff, col_diff in DIFFS:
            if 0 <= row + row_diff < n and 0 <= col + col_diff < n:
                min_cost = min(min_cost, seats[row + row_diff][col + col_diff])
            seats[row][col] = min_cost

    opening = deque([(row, col)])
    while opening:
        curr_row, curr_col = opening.popleft()

        for row_diff, col_diff in DIFFS:
            next_row = curr_row + row_diff
            next_col = curr_col + col_diff

            if not (0 <= next_row < n and 0 <= next_col < n):
                continue

            next_cost = seats[curr_row][curr_col] + rems[next_row][next_col]

            if next_cost < seats[next_row][next_col]:
                seats[next_row][next_col] = next_cost
                opening.append((next_row, next_col))

print(ans)
