from collections import deque N = int(input()) a = list((bin(i).count("1") for i in range(N+1))) inf = float("inf") dp = [inf, 1] + [inf]*(N+100) dq = deque([(1, 1)]) pop, append = dq.pop, dq.append while dq: v, steps = pop() if v == N: continue back, forward = v-a[v], v+a[v] steps += 1 if dp[back] > steps: dp[back] = steps append((back, steps)) if N >= forward and dp[forward] > steps: dp[forward] = steps append((forward, steps)) print(dp[N] if dp[N] < inf else -1)