def process(count, ni) if ni == $n $min = count if count < $min return end if count >= $n / 2 || count >= $min return end front = ni + $bit[ni] back = ni - $bit[ni] if front <= $n && $a[ni] == 0 $a[ni] = 1 process(count + 1, front) elsif $a[ni] == 0 $a[ni] = 1 end if back > 0 && $a[ni] <= 1 $a[ni] = 2 process(count + 1, back) elsif $a[ni] <= 1 $a[ni] = 2 end return end $n = gets.to_i $a = Array.new($n + 1, 0) $bit = Array.new($n + 1, 0) $min = 99999999 ($n + 1).times do |i| s = i.to_s(2) s.each_char do |c| $bit[i] += 1 if c == "1" end end process(1, 1) if $min < 99999999 puts $min else puts -1 end