N = int(input()) tmp = 0 for i in range(N): XY = list(map(int, input().split())) if XY[0] > XY[1]: print(-1) break elif i == N - 1: if XY[1] - XY[0] == tmp: print(tmp) else: print(-1) else: tmp = XY[1] - XY[0]