def str_len(s):
    a, b = list(map(int, s.split()))
    return a + b * 4

n = int(input())
cnt = 0
lens = [str_len(input()) for i in range(n)]
ml = max(lens)
bf = ml % 2
for l in lens:
    if l % 2 != bf:
        cnt = -1
        break
    cnt += (ml - l) // 2
print(cnt)