from sys import stdin def main(): N = int(input()) L = stdin.read().splitlines() num = [sum(a * 4 if i % 2 else a for i, a in enumerate(map(int, j.split()))) for j in L] judge = not len({map(lambda x: x % 2, num)}) - 1 if judge: print((max(num) * N - sum(num)) // 2) else: print(-1) main()