Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/saralash-ichma-ich-konvertlar

Nested envelopes

Hard 1000 ms 128 MB 67%
Share

There are $n$ envelopes, the $i$-th envelope has width $w_{i}$ and height $h_{i}$. One envelope can be put into another only if both its width and its height are strictly smaller (rotation is not allowed). At most how many envelopes can a chain of envelopes put one inside another consist of?

Envelopes as rectangles; the longest chain of envelopes nested inside each other is highlighted

Input

The first line contains a positive integer $n$. Each of the next $n$ lines contains two positive integers: $w_{i}$ and $h_{i}$.

Output

One integer — the largest number of envelopes in a chain.

Note

$1 \le n \le 2 \cdot 10^{5}$
$1 \le w_{i}, h_{i} \le 10^{9}$

Examples

Input #1
5
5 4
6 4
6 7
2 3
7 5
Output #1
3
Input #2
1
3 3
Output #2
1

Sign in to submit a solution

Signing up is free and takes a minute.