import collections
import itertools


def main():
    N = int(input())
    S = N + 2
    M = N * N
    cells = [[1] * (S) for _ in range(S)]
    costs = [[M] * S for _ in range(S)]
    total_cost = 0

    queue = collections.deque()

    for v in range(S - 1):
        costs[0][v] = 0
        costs[S - 1][v + 1] = 0
        costs[v + 1][0] = 0
        costs[v][S - 1] = 0

        cells[0][v] = 0
        cells[S - 1][v + 1] = 0
        cells[v + 1][0] = 0
        cells[v][S - 1] = 0

        queue.append((v, 0))
        queue.append((v + 1, S - 1))
        queue.append((0, v + 1))
        queue.append((S - 1, v))

    while len(queue) != 0:
        x, y = queue.popleft()
        c = costs[y][x]

        if x > 0 and costs[y][x - 1] == M:
            costs[y][x - 1] = c + 1
            queue.append((x - 1, y))

        if x < S - 1 and costs[y][x + 1] == M:
            costs[y][x + 1] = c + 1
            queue.append((x + 1, y))

        if y > 0 and costs[y - 1][x] == M:
            costs[y - 1][x] = c + 1
            queue.append((x, y - 1))

        if y < S - 1 and costs[y + 1][x] == M:
            costs[y + 1][x] = c + 1
            queue.append((x, y + 1))

    for i in map(int, input().split()):
        x, y = divmod(i - 1, N)
        x, y = x + 1, y + 1
        total_cost += costs[y][x] - 1

        costs[y][x] -= 1
        cells[y][x] = 0
        queue = collections.deque()
        queue.append((x, y))

        while len(queue) != 0:
            x, y = queue.popleft()

            for ax, ay in ((1, 0), (0, -1), (0, 1), (0, -1)):
                nx, ny = x + ax, y + ay
                c = costs[y][x] + cells[ny][nx]

                if costs[ny][nx] > c:
                    costs[ny][nx] = c
                    queue.append((nx, ny))

    print(total_cost)


if __name__ == '__main__':
    main()
