#include //#include using namespace std; //using namespace atcoder; //using mint = modint998244353; //const int mod = 998244353; //using mint = modint1000000007; //const int mod = 1000000007; const int INF = 1e9; //const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } bool chk(const vector

&v, int k) { int pre = 0; int cnt = 0; rep2(i, 1, v.size()) { if (v[pre].second <= v[i].first) { cnt++; pre = i; } } return cnt >= k; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector

d(n); rep(i, n)cin >> d[i].first >> d[i].second; sort(all(d), [](const P x, const P &y) {return x.second < y.second; }); if (!chk(d, k)) { cout << -1 << endl; return 0; } vectornext(n, -1);// todo 高速化 { struct SubD { int l, r, idx; SubD() {}; SubD(int l, int r, int idx) : l(l), r(r), idx(idx) {}; }; vectorsubD(n); rep(i, n) { subD[i] = SubD(d[i].first, d[i].second, i); } int mn = INF; int idx = -1; sort(all(subD), [](const SubD x, const SubD &y) {return x.l < y.l; }); rrep(i, n) { while (!subD.empty() && d[i].second <= subD.back().l) { auto top = subD.back(); if (chmin(mn, top.r))idx = top.idx; subD.pop_back(); } next[i] = idx; } } /*rep(i, n) { rep(j, n) { if (d[i].second > d[j].first)continue; if (-1 == next[i])next[i] = j; else if (d[j].second < d[next[i]].second)next[i] = j; } }*/ // dp[i][j]:=iから初めて2^j個数選ぶ時の最後に選んだindex vector dp(n, vector(20, -1)); rep(i, n)dp[i][0] = i; rep2(i, 1, 20) { rep(j, n) { int x0 = dp[j][i - 1]; if (-1 == x0)continue; int x1 = next[x0]; if (-1 == x1)continue; int x2 = dp[x1][i - 1]; dp[j][i] = x2; } } int ans = INF; rep(i, n) { int ck = k; int ci = i; bool chk = true; rep(j, 20) { if (1 == ck % 2) { if (i != ci)ci = next[ci]; if (-1 == ci) { chk = false; break; } ci = dp[ci][j]; if (-1 == ci) { chk = false; break; } } ck /= 2; } if (chk) { int val = d[ci].second - d[i].first; chmin(ans, val); //cout << i << " " << ci << endl; } } /*rep(i, n) { rep(j, 5) { cout << dp[i][j] << " "; }cout << endl; }*/ cout << ans << endl; return 0; }