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|A[i-1]+i==n||A[i-1]-i==n}.min break -1if !m h[m,r<