N = int(input()) def get_total(x): return sum(map(int,list("{0:b}".format(x)))) already = set() def go_next(now, mv_count): already.add(now) mv_count += 1 step = get_total(now) # 進める数 #print("now", now, "mv_count", mv_count, "step", step) go_m = now + step # 進む先 if go_m == N: # goal print(mv_count) exit(0) if go_m < N: go_next(go_m, mv_count) back_m = now - step # 戻る先 if back_m not in already: # 行ったことのあるところなら行かない if back_m > 0: go_next(back_m, mv_count) if N == 1: print(1) else: # 今=1, ステップ=1で再帰スタート go_next(1, 1) # 到達できなかった print(-1)