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