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

#GD0000060. Liên hoan phim (Movie Festival)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Liên hoan phim (Movie Festival)

Movie Festival

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

At a festival, nn movies have starting times aia_i and ending times bib_i. You want to watch as many entire movies as possible. You may watch another movie immediately when the previous one ends. Find the maximum number of movies you can watch in full.

Input

The first line contains nn. Each of the next nn lines contains integers ai,bia_i,b_i, the start and end times of movie ii.

Output

Print one integer: the maximum number of movies that can be watched in full.

Subtasks

  • Subtask 1 (20 points): 1≤n≤241\le n\le 24; 1≤ai<bi≤1091\le a_i<b_i\le 10^9 for every ii.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤ai<bi≤1091\le a_i<b_i\le 10^9 for every ii.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤ai<bi≤1091\le a_i<b_i\le 10^9 for every ii.

Examples

Input

3
3 5
4 9
5 8

Output

2

Explanation

The movies occupy [3,5][3,5], [4,9][4,9], and [5,8][5,8]. Watch the first until time 55, then the third from 55 to 88, for 22 movies. All three cannot be watched without overlap.