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(l - i for i, (l, _) in enumerate(lr, 1)) 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 min_k = get_min_k() if min_k is None: print(0) else: max_k = min(r - i for i, r in enumerate(sorted(r for _, r in lr), 1)) print(max_k - min_k + 1)