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