n = int(input()) base = [] for _ in range(n): a, b = map(int, input().split()) base_i = a + 4 * b base.append(base_i) # Check if all base_i have the same parity par = base[0] % 2 possible = True for num in base[1:]: if num % 2 != par: possible = False break if not possible: print(-1) else: max_base = max(base) total = 0 for num in base: diff = max_base - num total += diff // 2 print(total)