PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CC0000056. Lồng các phong bì (Russian Doll Envelopes)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Lồng các phong bì (Russian Doll Envelopes)

Russian Doll Envelopes

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

You have nn envelopes. An envelope fits inside another only when both its width and height are strictly smaller. Rotation is forbidden. Find the maximum number of envelopes in a nested chain.

Input

The first line contains nn, followed by nn pairs of envelope width wiw_i and height hih_i.

Output

Print the maximum number of envelopes in a nested chain.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le20; 1≤wi,hi≤1051\le w_i,h_i\le10^5.
  • Subtask 2 (30 points): 1≤n≤15001\le n\le1500; 1≤wi,hi≤1051\le w_i,h_i\le10^5.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le100000; 1≤wi,hi≤1051\le w_i,h_i\le10^5.

Examples

Input

4
5 4
6 4
6 7
2 3

Output

3

Explanation

A valid chain is (2,3) -> (5,4) -> (6,7) of length 3. Equal widths cannot nest.