n=gets.to_i+1 a=(0..n).map{|i|('%b'%i).count ?1};a[0]=1 r=[-1]*n;r[0]=0 n.times{|i|r.include?(i)&&n.times{|j| r[j]==i&&(x=j-a[j];y=j+j-x x*r[x]<0&&r[x]=i+1 y