n = int(input()) c = [] for i in range(n): a, b = map(int, input().split()) t = a + 4 * b c.append(t) ans = 0 maxc = max(c) for i in c: diff = maxc - i if diff % 2 == 0: ans += diff // 2 else: ans = -1 break print(ans)