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