1 #include <iostream> 2 #include <algorithm> 3 #define _for(i,a,b) for(int i = (a);i < b;i ++) 4 #define _rep(i,a,b) for(int i = (a);i > b;i --) 5 #define INF 0x3f3f3f3f 6 typedef long long ll; 7 using namespace std; 8 inline ll read() 9 { 10 ll ans = 0; 11 char ch = getchar(), last = ' '; 12 while(!isdigit(ch)) last = ch, ch = getchar(); 13 while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); 14 if(last == '-') ans = -ans; 15 return ans; 16 } 17 inline void write(ll x) 18 { 19 if(x < 0) x = -x, putchar('-'); 20 if(x >= 10) write(x / 10); 21 putchar(x % 10 + '0'); 22 } 23 struct section 24 { 25 int l; 26 int r; 27 bool operator < (section b) 28 { 29 if(l != b.l) 30 return l < b.l; 31 return r < b.r; 32 } 33 }; 34 int N,T; 35 section a[25003]; 36 int main() 37 { 38 N = read(),T = read(); 39 40 _for(i,1,N+1) 41 a[i].l = read(),a[i].r = read(); 42 43 sort(a+1,a+N+1); 44 45 int l = 0,i = 0; 46 int rnt = 0; 47 while(l < T) 48 { 49 int maxr = -1; 50 while(a[++i].l <= l+1 && i < N+1) 51 maxr = max(maxr,a[i].r); 52 if(i > N+1) 53 break; 54 if(maxr==-1) {printf("-1 ");return 0;} 55 rnt ++; 56 i --; 57 l = maxr; 58 } 59 printf("%d ",rnt); 60 return 0; 61 }