info

MAPS OS v1.1.4 © 2025

Glory to the Segtree

Glory to the Algorithm.

Salaryman FixGo home

Login to view input

no submission

Login to submit an answer

Problem Leaderboard

Problem Leaderboard

Salaryman Fix

Salaryman's Secret Schedule

A salaryman begging for help

Image generated by Google Gemini.

After identifying the full list of people who might know his secret, the salaryman is now on a mission: meet them face-to-face and convince them to keep quiet. Each person is only available during a specific time window, and each meeting must last exactly one hour.

Unfortunately, our salaryman is extremely busy and can only attend one meeting per hour. Now he needs your help to figure out the best possible schedule that lets him meet the maximum number of people.

Input

  • The first line contains a single integer n : the number of people who know the secret.
  • Each of the next n lines contains two integers starti at endi : the inclusive time window (in hours) during which person i is available for a meeting and starti < endi. If the time window ends at 4, the meeting must begin at 3 or before.

Output

Print a single integer: the maximum number of meetings the salaryman can conduct, such that:

  • Each meeting takes exactly one hour
  • Each meeting happens within the corresponding person's availability
  • He can attend only one meeting per hour

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ starti < endi ≤ 109

Example

Input:
4
1 2
2 3
3 4
1 4

Output:
3

Explanation

A possible valid meeting schedule:

  • Meet person 1 at hour 1 to 2
  • Meet person 2 at hour 2 to 3
  • Meet person 3 at hour 3 to 4

The salaryman can meet person 1, person 2, and person 3, all within their available time windows. Person 4 could not be squeezed in (hour 5 is outside their schedule), but that's okay, peace has been mostly restored.