from collections import deque def bfs(): INF = 1<<32 cost = [INF] * (N+1) cost[1] = 1 Queue = deque() Queue.append(1) while Queue: x = Queue.popleft() C = bin(x).count('1') if 1 <= x - C and cost[x] + 1 < cost[x-C]: cost[x-C] = cost[x] + 1 Queue.append(x-C) if x + C <= N and cost[x] + 1 < cost[x+C]: cost[x+C] = cost[x] + 1 Queue.append(x+C) return cost INF = 1<<32 N = int(input()) cost = bfs() if cost[N] == INF: cost[N] = -1 print(cost[N])