import collections
import itertools


class Cell(object):

    def __init__(self):
        self.value = None
        self.exist = 1
        self.neighbors = []


def main():
    N = int(input())
    cells = [[Cell() for _ in range(N)] for _ in range(N)]
    queue = collections.deque()
    count = 0

    for x, y in itertools.product(range(N), repeat=2):
        c = cells[y][x]

        if x == 0:
            c.value = 0
        else:
            c.neighbors.append(cells[y][x - 1])

        if x == N - 1:
            c.value = 0
        else:
            c.neighbors.append(cells[y][x + 1])

        if y == 0:
            c.value = 0
        else:
            c.neighbors.append(cells[y - 1][x])

        if y == N - 1:
            c.value = 0
        else:
            c.neighbors.append(cells[y + 1][x])

        if c.value == 0:
            queue.append(c)

    while len(queue) != 0:
        c = queue.popleft()

        for n in c.neighbors:
            if n.value is None:
                n.value = c.value + 1
                queue.append(n)

    queue = collections.deque()

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

        cells[y][x].value -= 1
        cells[y][x].exist = 0

        queue.append(cells[y][x])

        while len(queue) != 0:
            c = queue.popleft()

            for n in c.neighbors:
                if n.value > c.value + n.exist:
                    n.value = c.value + n.exist
                    queue.append(n)

    print(count)


if __name__ == '__main__':
    main()
