def main():
    import sys
    input = sys.stdin.readline
    from collections import deque
    n = int(input())
    p = list(map(int, input().split()))

    distance = [[min(i, j, n - i - 1, n - j - 1) for i in range(n)] for j in range(n)]
    check = [[1] * n for i in range(n)]
    p = [((i - 1) // n, (i - 1) % n) for i in p]

    def move(x, y, nx, ny):
        if distance[nx][ny] > distance[x][y] + check[x][y]:
            distance[nx][ny] = distance[x][y] + check[x][y]
            d.append((nx, ny))

    ans = 0
    for i, j in p:
        ans += distance[j][i]
        d = deque()
        d.append((j, i))
        check[j][i] = 0
        while len(d):
            x, y = d.popleft()
            if x != 0:
                move(x, y, x - 1, y)
            if x != n - 1:
                move(x, y, x + 1, y)
            if y != 0:
                move(x, y, x, y - 1)
            if y != n - 1:
                move(x, y, x, y + 1)

    print(ans)


if __name__ == '__main__':
    main()