N = int(raw_input()) max = None ans = 0 for i in range(N): spc, tab = map(int, raw_input().split()) length = spc + 4 * tab if max is None: max = length elif (max - length)%2 == 1: ans = -1 break else: ans += abs(max - length)/2 print ans