Time limit
2s
Memory limit
128 MB
Several segments are given on the x-axis. Each segment is written as [L_i, R_i], meaning that it starts at (L_i) and ends at (R_i). Choose some of these segments so that the whole interval [0, M] is covered. Find the minimum number of segments needed to cover [0, M] completely.
The first line contains an integer (M)((1 \le M \le 50,000)). Each following line contains two integers (L_i) and (R_i)((|L_i|, |R_i| \le 50,000)) describing one segment. The segment list ends with 0 0. There are at most 100,000 segments.
Print the minimum number of segments needed to cover [0, M] completely. If it is impossible, print 0.