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

#BF0000033. Đoạn nguyên và hai điểm (Integer Intervals)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đoạn nguyên và hai điểm (Integer Intervals)

Integer Intervals

Source: POJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn closed integer intervals [ai,bi][a_i,b_i], each with at least two integers, choose a minimum-size set containing at least two chosen integers in every interval.

Input

First line nn, followed by nn pairs ai,bia_i,b_i.

Output

Print the minimum number of chosen integers.

Subtasks

  • Subtask 1 (20 points): 1≤n≤20;0≤ai<bi≤301\le n\le20; 0\le a_i<b_i\le30.
  • Subtask 2 (30 points): 1≤n≤200;0≤ai<bi≤20001\le n\le200; 0\le a_i<b_i\le2000.
  • Subtask 3 (50 points): 1≤n≤1000;0≤ai<bi≤100001\le n\le1000; 0\le a_i<b_i\le10000.

Examples

Input

3
1 3
2 5
4 6

Output

4

Explanation

For instance {2,3,5,6}\{2,3,5,6\} covers each given interval with at least two selected integers. The first interval needs two integers not available in the last interval, so at least four are necessary.