There are n closed intervals \[l_1,r_1],\[l_2,r_2],…,\[l_n,r_n]. You need to choose m intervals such that the m intervals have nonempty intersection. In other words, there exists some x such that for any selected interval \[l_i,r_i], l_i≤x≤r_i.
The cost of selecting a collection of m intervals with nonempty intersection is the maximum length of the m intervals minus the minimum length of the m intervals. The length of interval \[l_i,r_i] is r_i−l_i.
Compute the minimum cost of choosing m intervals with nonempty intersection. If this is impossible, output -1.
The first line consists of two positive integers n,m separated by a single space where n is the total number of intervals and m is the number of intervals we are going to choose. It is guaranteed that 1≤m≤n.
In the following n lines, each line consists of two integers l_i,r_i separated by a space denoting the left and right endpoints of an interval.
Output a line with an integer denoting the minimum cost.
| Test case | n= | m= | l_i,r_i |
|---|---|---|---|
| 1 | 20 | 9 | 0≤l_i≤r_i≤100 |
| 2 | 10 | ||
| 3 | 199 | 3 | 0≤l_i≤r_i≤100,000 |
| 4 | 200 | ||
| 5 | 1,000 | 2 | |
| 6 | 2,000 | ||
| 7 | 199 | 60 | 0≤l_i≤r_i≤5,000 |
| 8 | 200 | 50 | |
| 9 | 0≤l_i≤r_i≤109 | ||
| 10 | 1,999 | 500 | 0≤l_i≤r_i≤5,000 |
| 11 | 2,000 | 400 | |
| 12 | 500 | 0≤l_i≤r_i≤109 | |
| 13 | 30,000 | 2,000 | 0≤l_i≤r_i≤100,000 |
| 14 | 40,000 | 1,000 | |
| 15 | 50,000 | 15,000 | |
| 16 | 100,000 | 20000 | |
| 17 | 200,000 | 0≤l_i≤r_i≤109 | |
| 18 | 300,000 | 50,000 | |
| 19 | 400,000 | 90,000 | |
| 20 | 500,000 | 200,000 |