from collections import deque def bitCount(n): return bin(n).count("1") N = int(input()) Node = (1,1) List = [1] Queue = deque([Node]) while Queue: n,count = Queue.popleft() if n == N: print(count) exit() bit_count = bitCount(n) if n+bit_count <= N and n + bit_count not in List: List.append(n+bit_count) Queue.append((n+bit_count,count + 1)) if n-bit_count > 1 and n - bit_count not in List: List.append(n-bit_count) Queue.append((n-bit_count,count + 1)) print(-1)