1 //It is made by Awson on 2017.10.17
2 #include <set>
3 #include <map>
4 #include <cmath>
5 #include <ctime>
6 #include <cmath>
7 #include <stack>
8 #include <queue>
9 #include <vector>
10 #include <string>
11 #include <cstdio>
12 #include <cstdlib>
13 #include <cstring>
14 #include <iostream>
15 #include <algorithm>
16 #define LL long long
17 #define Min(a, b) ((a) < (b) ? (a) : (b))
18 #define Max(a, b) ((a) > (b) ? (a) : (b))
19 #define sqr(x) ((x)*(x))
20 #define Lr(x) (x<<1)
21 #define Rr(x) (x<<1|1)
22 using namespace std;
23 const int N = 500000;
24
25 int n, m, ans = 2e9;
26 struct tt {
27 int l, r, val;
28 bool operator < (const tt &b) const{
29 return val < b.val;
30 }
31 }a[N+5];
32 struct ss {
33 int val, id, op;
34 bool operator < (const ss &b) const{
35 return val < b.val;
36 }
37 }b[(N<<1)+5];
38 struct segment {
39 int sgm[(N<<3)+5], lazy[(N<<3)+5];
40 void pushdown(int o) {
41 sgm[Lr(o)] += lazy[o], sgm[Rr(o)] += lazy[o];
42 lazy[Lr(o)] += lazy[o], lazy[Rr(o)] += lazy[o];
43 lazy[o] = 0;
44 }
45 void update(int o, int l, int r, int a, int b, int key) {
46 if (a <= l && r <= b) {
47 sgm[o] += key, lazy[o] += key;
48 return;
49 }
50 pushdown(o);
51 int mid = (l+r)>>1;
52 if (a <= mid) update(Lr(o), l, mid, a, b, key);
53 if (b > mid) update(Rr(o), mid+1, r, a, b, key);
54 sgm[o] = Max(sgm[Lr(o)], sgm[Rr(o)]);
55 }
56 }T;
57
58 void work() {
59 scanf("%d%d", &n, &m);
60 for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].l, &a[i].r), a[i].val = a[i].r-a[i].l;
61 sort(a+1, a+n+1);
62 for (int i = 1; i <= n; i++) {
63 b[(i<<1)-1].val = a[i].l, b[(i<<1)-1].id = i; b[i<<1].val = a[i].r, b[i<<1].id = i;
64 }
65 sort(b+1, b+2*n+1); b[0].val = -1;
66 for (int i = 1; i <= (n<<1); i++) b[i].op = b[i-1].op+(b[i].val != b[i-1].val);
67 for (int i = 1; i <= (n<<1); i++) {
68 if (b[i].val == a[b[i].id].l) a[b[i].id].l = b[i].op;
69 if (b[i].val == a[b[i].id].r) a[b[i].id].r = b[i].op;
70 }
71 int tol = b[n<<1].op, r = 0;
72 for (int i = 1; i <= n; i++) {
73 while (r < n && T.sgm[1] < m) {
74 r++; T.update(1, 1, tol, a[r].l, a[r].r, 1);
75 }
76 if (T.sgm[1] >= m) ans = Min(ans, a[r].val-a[i].val);
77 else break;
78 T.update(1, 1, tol, a[i].l, a[i].r, -1);
79 }
80 printf("%d\n", ans == 2e9 ? -1 : ans);
81 }
82 int main() {
83 work();
84 return 0;
85 }