gets res = catch(:impossible) do ary = [] max_sp = 0 while line = gets do a, b = line.split.map(&:to_i) sp = a + 4 * b ary << sp max_sp = sp if sp > max_sp end count = 0 ary.each do |sp| q, r = (max_sp - sp).divmod(2) throw :impossible, -1 unless r == 0 count += q end count end puts res