cswork / OAOpen practice workspace

SnowflakeOA #18

Smallest Set Covering Intervals

Estimated HardJudge-ready

Topics (AI-labelled)GreedyPrefix & Ranges

Given closed integer intervals, find the smallest set of integers such that every interval contains at least two distinct integers from the set. The original text allows single-point intervals but does not define the output when there is no solution; this site's protocol specifies outputting −1 in that case, without removing any originally valid input range.

Input

The first line contains n, followed by n lines of first last. 1≤n≤100000, 0≤first≤last≤10⁹.

Output

Output the single integer required by the problem.

Example 1

Input:

3
0 2
1 3
2 3

Output:

3

Example 2

Input:

1
0 0

Output:

-1

Example 3

Input:

3
1 3
1 4
2 5

Output:

2

Example notes

The answers to the three public examples are, in order: 3; -1; 2. All examples have been verified by independent small-scale enumeration or state search.

Editorial

Loading solution…