def cbit(x);x.to_s(2).chars.count("1");end n=gets.to_i a=Array.new(n+1) a[1]=1 x=1 stack=[] while x!=n b=cbit(x) if x-b>0 &&!a[x-b];stack.push(x-b);a[x-b]=a[x]+1;end if x+b<=n&&!a[x+b];stack.push(x+b);a[x+b]=a[x]+1;end if stack.empty?;a[n]=-1;break;end x=stack.shift end p a[n]