gole = gets.to_i current = 1 root_count = 1 until current >= gole do move = current.to_s(2).count("1") root_count += 1 if root_count > gole then root_count = -1 break elsif current + move == gole then break elsif current + move > gole then current -= move elsif current + move < gole then current += move end end puts root_count