N = int(input()) now = 0 flag = 0 for i in range(N): x,y = map(int,input().split()) S = y-x if S<0: print(-1) flag += 1 break else: if i == 0: now = y-x else: if S != now: print(-1) flag += 1 break if flag == 0: print(now)