#P2684. Minimum Cleaning Cows

    ID: 15949 Type: Default 1000ms 256MiB

Minimum Cleaning Cows

Minimum Cleaning Cows

FJ has N cows available to perform cleaning, and he divides the day into T time periods. Each cow is available during a specific time interval. FJ wants to minimize the number of cows that actually perform cleaning while ensuring that every time period is covered by at least one cow.

Formally, you are given a target interval \([0, T]\) and N intervals \([L_i, R_i]\) (with \(0 \le L_i < R_i \le T\)) representing the time periods during which each cow can clean. Your task is to select the minimum number of cows such that the union of their intervals covers the entire interval \([0, T]\). If it is impossible to cover the entire period, output \(-1\).

inputFormat

The first line contains two integers N and T (with \(1 \le N \le 25000\) and \(1 \le T \le 10^6\)). Each of the following N lines contains two integers \(L\) and \(R\) (with \(0 \le L < R \le T\)), representing the time interval during which a cow is available to clean.

outputFormat

Output the minimum number of cows required to cover the entire interval \([0, T]\). If it is impossible to cover the interval, output \(-1\).

sample

3 10
0 5
4 10
5 8
2

</p>