n = gets.to_i ans = Array.new(n,10000) a = [[1,1]] until a.empty? x = a.pop if ans[x[0] - 1] <= x[1] next end 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 a.push([x[0] - tmp, x[1] + 1]) end end if ans[n - 1] == 10000 p -1 else p ans[n - 1] end