def max(a,b); a > b ? a : b; end N = gets.to_i A = [0, 0] max_w = 0 L = N.times.map do a,b = gets.split.map(&:to_i) A[a % 2] += 1 (a + 4 * b).tap do |l| max_w = max(max_w, l) end end if A[0] > 0 && A[1] > 0 puts -1 else puts L.inject(0){|s,l| s + (max_w - l) / 2 } end