
import queue
import sys
input = sys.stdin.buffer.readline


def main():
    N = int(input())
    aa = list(map(int, input().split()))

    mm = [[0] * N for _ in range(N)]

    for i in range((N + 1) // 2):
        j = N - i - 1

        for x in range(i, j + 1):
            mm[i][x] = i
            mm[j][x] = i
            mm[x][i] = i
            mm[x][j] = i

    is_none = [[False] * N for _ in range(N)]

    ans = 0

    for a in aa:
        x = (a - 1) % N
        y = (a - 1) // N

        ans += mm[y][x]

        is_none[y][x] = True

        mm[y][x] -= 1

        q = queue.Queue()
        q.put((x, y, mm[y][x]))
        while not q.empty():
            x0, y0, n = q.get()

            xx = [x0, x0, x0 + 1, x0 - 1]
            yy = [y0 + 1, y0 - 1, y0, y0]
            for x, y in zip(xx, yy):
                if x < 0 or N <= x or y < 0 or N <= y:
                    continue

                if is_none[y][x]:
                    if mm[y][x] > n:
                        mm[y][x] = n
                        q.put((x, y, mm[y][x]))
                else:
                    if mm[y][x] > n + 1:
                        mm[y][x] = n + 1
                        q.put((x, y, mm[y][x]))

    return ans


if __name__ == "__main__":
    ans = main()
    print(ans)
