import heapq from heapq import heappop, heappush, heapify from copy import deepcopy as dcopy N = int(input()) # N = 5000 stack = deque() stack.append((1, 0)) # count, position, arrived, root D = [1] + [float("inf")]*(N-1) # saitankyorimap flag = False while len(stack) != 0: count, position = stack.popleft() number = position + 1 if number == N: print(count) flag = True break move_size = bin(number).count("1") for move in (move_size, - move_size): new_position = position + move new_count = count + 1 if (0 <= new_position <= N-1) and D[new_position] > count: D[new_position] = count stack.append((new_count, new_position)) if not flag: print(-1)