n=0..k=gets.to_i r=[0] n.map{|i|r&[i]==[i]&&n.map{|j|r[j]&&(x=j<1?-1:('%b'%j).count(?1) r[j+x]||=i+1 r[j-x]||=i+1)}} p r[k]||~0