def foo(N): distance = [bin(_).count('1') for _ in range(N+1)] shortest = [N for _ in range(N+1)] shortest[1] = 1 changed = True while changed: changed = False for masu in range(1, N+1): dest = masu+distance[masu] if dest <= N: if shortest[masu]+1 < shortest[dest]: shortest[dest] = shortest[masu]+1 changed = True dest = masu-distance[masu] if dest >= 1: if shortest[masu]+1 < shortest[dest]: shortest[dest] = shortest[masu]+1 return -1 if shortest[N]==N else shortest[N] print(foo(int(input())))