import heapq n = int(input()) lr = [tuple(map(int, input().split())) for _ in range(n)] def check(k: int): pq = [] j = 0 for v in range(k + 1, k + n + 1): while j < n and lr[j][0] <= v: heapq.heappush(pq, lr[j][1]) j += 1 if pq[0] < v: return False heapq.heappop(pq) return True def get_min_k(): lr.sort(key=lambda t: t[0]) k = max(lr[i][0] - (i + 1) for i in range(n)) return k if check(k) else None min_k = get_min_k() if min_k is None: print(0) else: ok, ng = min_k, 10 ** 9 while ng - ok > 1: k = (ok + ng) >> 1 if check(k): ok = k else: ng = k print(ok - min_k + 1)