from collections import deque N, *A = map(int, open(0).read().split()) ans = 0 Q = deque(zip(*[iter(A)] * 2)) while Q: lh, lt = Q[0] l = lh - lt rt, rh = Q[-1] r = rh - rt if l < r: Q.pop() else: Q.popleft() ans += max(l, r) print(ans)