goal = int(input()) # goal_1bit_num = bin(goal) # print(goal_1bit_num.count('1')) stepped = set() maybe_answers = set() def step(position, count): if position in stepped: return else: stepped.add(position) if position not in range(1, goal + 1): return if len(maybe_answers) > 0 and count < min(maybe_answers): return if position == goal: maybe_answers.add(count) return position_bin = bin(position) step(position + position_bin.count('1'), count + 1) step(position - position_bin.count('1'), count + 1) step(1, 1) if len(maybe_answers) == 0: print('-1') else: print(min(maybe_answers))