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)]) reachables[1] = True while(q): n, c = q.popleft() if n == N: routes.append(c) break else: back, go = moves[n]* -1 + n, moves[n] + n for target in [go, back]: if (1 < target <= N) and not reachables[target]: reachables[target] = True q.append((target, c+1)) if len(routes) == 0: print(-1) # unreachable else: print(min(routes))