class BitSugoroku def initialize(n) @n = n end def saitan if @n == 1 return 1 end i = 1 kazoeta = [1] list = [1] while list.size > 0 i += 1 next_list = [] list.each do |x| bits = num_bit(x) add_n = x + bits del_n = x - bits if add_n == @n return i elsif (add_n < @n) && (! kazoeta.include?(add_n)) kazoeta << add_n next_list << add_n end if (! kazoeta.include?(del_n)) kazoeta << del_n next_list << del_n end end list = next_list end return -1 end def num_bit(x) count = 0 while x > 0 count += x & 1 x = x >> 1 end return count end end n = gets.to_i bs = BitSugoroku.new(n) puts bs.saitan