import heapq n = int(input()) lr = [tuple(map(int, input().split())) for _ in range(n)] def get_min_k(): lr.sort(key=lambda t: t[0]) k = max(lr[i][0] - (i + 1) for i in range(n)) 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 None heapq.heappop(pq) return k M = 10 ** 9 + 1 min_k = get_min_k() if min_k is None: print(0) else: rs = [r for _, r in lr] rs.sort() max_k = min([r - (i + 1) for i, r in enumerate(rs)]) print(max_k - min_k + 1)