from sys import stdin def main(): N = int(input()) L = stdin.read().splitlines() x = [sum(a * 4 if i % 2 else a for i, a in enumerate(map(int, j.split()))) for j in L] x.sort(reverse=1) ans, maxn = 0, x[0] for i in x: n = maxn - i if n % 2: print(-1) return else: ans += n // 2 print(ans) main()