#include #include #include #include #include #include int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector> lr(n); for (auto& [l, r] : lr) std::cin >> l >> r; std::sort(lr.begin(), lr.end()); int k = 0; for (int i = 1; i <= n; ++i) k = std::max(k, lr[i - 1].first - i); std::cerr << "min k = " << k << std::endl; int kr = 1000000000; for (int i = 0; i < n; ++i) kr = std::min(kr, lr[i].second - 1); int ans = 0; for (; k <= kr; ++k) { bool ok = true; std::priority_queue, std::greater> pq; for (int i = 1, j = 0; i <= n; ++i) { while (j < n and lr[j].first <= k + i) { pq.push(lr[j++].second); } if (pq.empty() or pq.top() < k + i) { ok = false; break; } pq.pop(); } if (ok) { ++ans; } if (ans and not ok) { std::cerr << "max k = " << k - 1 << std::endl; break; } } std::cout << ans << std::endl; }