def index(xs, x) left = 0 right = xs.size 100.times do i = (left + right) / 2 if xs[i] < x left = i else right = i end end left end n = gets.to_i ds = gets.chomp.split.map(&:to_i).sort x, y = gets.chomp.split.map(&:to_i) r = x.abs + y.abs if r.zero? p 0 elsif ds.any? {|d| d == r} p 1 elsif r.even? d_max = ds.max if r <= d_max + d_max p 2 else p -1 end else es = ds.select(&:even?).sort os = ds.select(&:odd?).sort e_max = es.max o_max = os.max if e_max == nil or o_max == nil p -1 exit end os.each do |d| diff = (r - d).abs i = index(es, diff) (i-3..i+3).each do |j| d2 = es[j] next if d2 == nil if (d - d2).abs <= r and r <= d2 + d2 p 2 exit end end end es.each do |d| diff = (r - d).abs i = index(os, diff) (i-3..i+3).each do |j| d2 = os[j] next if d2 == nil if (d - d2).abs <= r and r <= d2 + d2 p 2 exit end end end p -1 end