n = gets.to_i ans = Array.new(n,-1) a = [[1,1]] min = 100000 until a.empty? x = a.pop if x[0] == n min = [min, x[1]].min elsif ans[x[0] - 1] != -1 next else ans[x[0] - 1] = x[1] tmp = x[0].to_s(2).count('1') if x[0] + tmp <= n a.push([x[0] + tmp, x[1] + 1]) end if x[0] - tmp > 0 a.push([x[0] - tmp, x[1] + 1]) end end end if min == 100000 p -1 else p min end