Under construction

Still being built. Things may change

ICPC Team Generation

Time limit1sMemory limit1024 MB

Summary
Form the most teams of three where each member's two teammates have ranks inside that member's [a, b] range, with a and b nondecreasing in rank.
Level

Medium6 of 10

Topics
Dynamic programming, Intervals
Solved
No attempts yet

Problem

Vi is a coach for her university's ICPC organization. She is building teams for the upcoming regional contest. Her university recently competed in the North America Qualifier (NAQ), and Vi uses the results and each person's preferences to form as many teams of three as possible for regionals.

There are nn people from the university who competed in the NAQ. Each person has a unique rank from 11 to nn. The person at rank rr has two parameters, ara_r and brb_r, with ar≤r≤bra_r \le r \le b_r. Their two teammates must have a rank between ara_r and brb_r, inclusive. Teams must have exactly three people.

Because of the collaborative environment, for every pair of people at ranks ii and jj with i<ji < j, ai≤aja_i \le a_j and bi≤bjb_i \le b_j hold.

Compute the maximum number of teams Vi can send to regionals.

Input

The first line contains a single integer nn (3≤n≤503 \le n \le 50), the number of competitors in the local contest.

Each of the next nn lines contains two integers ara_r and brb_r (ar≤r≤bra_r \le r \le b_r), where rr is the competitor's rank. These are the limits on the ranks of the teammates for this competitor. Competitors are given in rank order, from 11 to nn. If i<ji < j, then ai≤aja_i \le a_j and bi≤bjb_i \le b_j.

Output

Output a single integer, the maximum number of teams Vi can send to the regional contest.

Examples1

  1. Example 1

    Input
    6
    1 2
    1 2
    2 5
    2 6
    2 6
    5 6
    
    Expected output
    1