N = int(input()) A, B = [0] * N, [0] * N ma = 0 for i in range(N): A[i], B[i] = map(int, input().split()) ma = max(ma, A[i] + 4 * B[i]) ans = 0 for i in range(N): v = A[i] + 4 * B[i] if (ma - v) % 2: print(-1) exit() ans += ma - v print(ans // 2)