N=gets.to_i A=(1..N).map{|i|i.to_s(2).count ?1} h=->n,r{ if n < 4 ([*1..n]+r|[]).size else b=n.bit_length m=([*n-b+1..N]-r).select{|i|i+A[i-1]==n||i-A[i-1]==n}.min break -1if !m h[m,r<