#include using namespace std; using i64 = int64_t; const i64 INF = LLONG_MAX / 4; void chmin(i64& a, i64 b) { if(a > b) a = b; } int main() { cin.tie(0)->sync_with_stdio(0); i64 N, K; cin >> N >> K; vector> A(N); map nx_end; for(auto& [L, R] : A) { cin >> L >> R; chmin(nx_end.try_emplace(L, R).first->second, R); } { i64 mn = INF; for(auto& [key, val] : nx_end | views::reverse) { chmin(mn, val); val = mn; } } map nx[18]; nx[0] = nx_end; for(i64 l = 0; l + 1 < 18; l++) { auto p = nx[l].begin(); for(auto [key, val] : nx[l]) { while(p != nx[l].end() && p->first < val) p++; if(p == nx[l].end()) break; nx[l + 1].emplace_hint(nx[l + 1].end(), key, p->second); } } i64 ans = INF; for(const i64 L : nx[0] | views::keys) { i64 R = L; for(i64 l = 18; l--; ) if(K & 1 << l) { auto p = nx[l].lower_bound(R); if(p == nx[l].end()) { if(ans == INF) ans = -1; cout << ans << endl; return 0; } R = p->second; } chmin(ans, R - L); } cout << ans << endl; }