#include using namespace std; #include using namespace atcoder; #define rep(i, n) for(int i=0;i<(n);++i) #define rep1(i, n) for(int i=1;i<=(n);i++) #define ll long long using mint = modint998244353; using P = pair; using lb = long double; using T = tuple; #ifdef LOCAL # include # define dbg(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define dbg(...) (static_cast(0)) #endif // Coodinate Compression // https://youtu.be/fR3W5IcBGLQ?t=8550 template struct CC { bool initialized; vector xs; CC(): initialized(false) {} void add(T x) { xs.push_back(x);} void init() { sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(),xs.end()),xs.end()); initialized = true; } int operator()(T x) { if (!initialized) init(); return upper_bound(xs.begin(), xs.end(), x) - xs.begin() - 1; } T operator[](int i) { if (!initialized) init(); return xs[i]; } int size() { if (!initialized) init(); return xs.size(); } }; int op(int a, int b) { return min(a,b); } int e(){ return 1e9; } int main() { int n, k; cin >> n >> k; vector> l(n); rep(i,n) cin >> l[i].first >> l[i].second; CC cc; rep(i,n) { cc.add(l[i].first); cc.add(l[i].second); } int m = cc.size(); segtree seg(m); rep(i,n){ seg.set(cc(l[i].first), min(seg.get(cc(l[i].first)), cc(l[i].second))); } vector> dp(31, vector(m, -1)); rep(i,n){ dp[0][cc(l[i].first)] = seg.prod(cc(l[i].first), m); if(dp[0][cc(l[i].second)]==1e9) dp[0][cc(l[i].second)] = -1; dp[0][cc(l[i].second)] = seg.prod(cc(l[i].second), m); if(dp[0][cc(l[i].second)]==1e9) dp[0][cc(l[i].second)] = -1; } dbg(dp); rep(k,30){ rep(j,m){ if(dp[k][j]==-1) continue; dp[k+1][j] = dp[k][dp[k][j]]; } } int ans = 2e9; dbg(dp); rep(i,n){ int now = cc(l[i].first); dbg(now); dbg(k); rep(j,30){ if(k>>j&1) { if(now==-1) continue; now = dp[j][now]; dbg(now); dbg(j); } } dbg(1,now); if(now==-1) continue; ans = min(ans, cc[now]-l[i].first); } if(ans==2e9) ans = -1; cout << ans << endl; return 0; }