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] odd = all(map(lambda x: x % 2, num)) even = not any(map(lambda x: x % 2, num)) if even or odd: print((max(num) * N - sum(num)) // 2) else: print(-1) main()