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