#pragma GCC optimize("O2") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define int ll #define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1) #define INT128_MIN (-INT128_MAX - 1) namespace R = std::ranges; namespace V = std::views; using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair; using pll = pair; using tiii = tuple; using ldb = long double; //#define double ldb template ostream& operator<<(ostream& os, const pair pr) { return os << pr.first << ' ' << pr.second; } template ostream& operator<<(ostream& os, const array &arr) { for(const T &X : arr) os << X << ' '; return os; } template ostream& operator<<(ostream& os, const vector &vec) { for(const T &X : vec) os << X << ' '; return os; } signed main() { ios::sync_with_stdio(false), cin.tie(NULL); int n, k; cin >> n >> k; vector> seg(n); for(auto &[l, r] : seg) cin >> l >> r; sort(seg.begin(), seg.end(), [](array l, array r) { return l[1] > r[1]; }); vector> cand(n); for(int i = 0; i < n; i++) cand[i] = {seg[i][0], seg[i][1], i}; R::sort(cand); vector jp(18, vector(n, -1)); int mnR = INT_MAX, id = -1; for(int i = 0; i < n; i++) { auto [l, r] = seg[i]; while(!cand.empty() and cand.back()[0] >= r) { auto [lp, rp, j] = cand.back(); if (rp < mnR) mnR = rp, id = j; cand.pop_back(); } jp[0][i] = id; } for(int i = 1; i < 18; i++) for(int j = 0; j < n; j++) if (jp[i - 1][j] != -1) jp[i][j] = jp[i - 1][jp[i - 1][j]]; int ans = INT_MAX; for(int i = 0; i < n; i++) { int j = i; for(int bit = 0; bit < 18; bit++) if (j != -1 and ((k - 1) >> bit & 1)) j = jp[bit][j]; if (j != -1) ans = min(ans, seg[j][1] - seg[i][0]); } cout << (ans == INT_MAX ? -1 : ans) << '\n'; return 0; }