The Party Photographer
Alice has been hired to photograph Bob's birthday party. The client has one demand: every guest must appear in at least one photo. Film is expensive, so Alice wants to take as few photos as possible.
Guest i arrives at time a_i and leaves at time b_i, and is at the party during the whole
closed time range from a_i to b_i, including both of those instants. Alice can take a photo at
any instant t (not necessarily an integer), and the photo captures every guest who is at the
party at instant t. In particular, a guest who arrives at the exact instant another guest leaves
is at the party at the same moment as that guest, so one photo taken at that instant captures both.
Photos take no time, and any number of photos may be taken, at any instants.
Find the minimum number of photos Alice needs so that every guest appears in at least one of them.
Input
The first line contains an integer n (1 ≤ n ≤ 2·10^5), the number of guests.
Each of the next n lines contains two integers a_i and b_i (0 ≤ a_i ≤ b_i ≤ 10^9), the
arrival and leaving times of guest i. A guest may arrive and leave at the same instant
(a_i = b_i), and several guests may have identical times.
Output
Print one integer: the minimum number of photos.
Notes
In the first example, a photo at time 4 captures guests 1, 2 and 3 (guest 1 leaves at 4 and guest 3 arrives at 4, so both are there), and a photo at time 9 captures guests 4 and 5. No single instant has all five guests present, so 2 photos are needed.
In the second example, nobody's stay overlaps with anyone else's, so every guest needs a photo of their own.