from collections import deque def bfs(N, INF): 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(N, INF) if Cost[N] == INF: print(-1) else: print(Cost[N])