from collections import deque N = int(input()) v = deque([0] * N) q = deque() q.append(1) v[0] = 1 result = False if N == 1: print(1) result = True else: while(q): bin_count = bin(int(q[0])).count('1') v_next = q[0] + bin_count v_back = q[0] - bin_count if v_next <= N and v[v_next - 1] == 0: v[v_next - 1] = v[q[0] - 1] + 1 q.append(v_next) if v_next == N: print(v[N -1]) result = True break if 0 < v_back <= N and v[v_back - 1] == 0: v[v_back - 1] = v[q[0] - 1] + 1 q.append(v_back) q.popleft() if result != True: print(-1)