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 and XY[1] - XY[0] == tmp): print(tmp) else: tmp = XY[1] - XY[0]