#include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 998244353; // constexpr int MOD = 1000000007; constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1}; constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}; constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; int main() { constexpr int L = 18; int n, k; cin >> n >> k; map rl; while (n--) { int l, r; cin >> l >> r; if (const auto it = rl.find(r); it == rl.end()) { rl[r] = l; } else { chmax(it->second, l); } } vector tm; tm.reserve(rl.size() * 2); for (const auto& [r, l] : rl) { tm.emplace_back(l); tm.emplace_back(r); } ranges::sort(tm); tm.erase(unique(tm.begin(), tm.end()), tm.end()); const int m = tm.size(); vector dp(L, vector(m, m)); vector r_min(m); for (const auto& [r, l] : rl) { const int l_ind = distance(tm.begin(), ranges::lower_bound(tm, l)); const int r_ind = distance(tm.begin(), ranges::lower_bound(tm, r)); chmin(dp[0][l_ind], r_ind); } for (int i = m - 2; i >= 0; --i) chmin(dp[0][i], dp[0][i + 1]); REP(i, L - 1) REP(j, m) { if (dp[i][j] < m) chmin(dp[i + 1][j], dp[i][dp[i][j]]); } int ans = INF; REP(i, m) { int cur = i; for (int bit = 0, j = k; j > 0 && cur < m; ++bit, j /= 2) { if (j & 1) cur = dp[bit][cur]; } if (cur >= m) break; chmin(ans, tm[cur] - tm[i]); } cout << (ans == INF ? -1 : ans) << '\n'; return 0; }