n = int(input()) s_list = [] parity = None possible = True for _ in range(n): a, b = map(int, input().split()) s = a + 4 * b s_list.append(s) if parity is None: parity = s % 2 else: if s % 2 != parity: possible = False if not possible: print(-1) else: max_s = max(s_list) total = 0 for s in s_list: diff = max_s - s total += diff // 2 print(total)