from collections import deque import numpy as np N = int(input()) moves = [bin(x).count('1') for x in range(N+1)] routes = [] reachables = np.zeros(N+1, dtype=bool) q = deque([(1,1)]) while(len(q)): n, c = q.pop() reachables[n] = True if n == N: routes.append(c) else: back, go = -1 * moves[n] + n, moves[n] + n for target in [back, go]: if (1 < target <= N) and not reachables[target]: q.append((target, c+1)) if len(routes) == 0: print(-1) # unreachable else: print(max(routes))