Rt
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct Node{
int x,y;
}a[25010];
bool cmp(Node p1,Node p2){
return p1.y<p2.y;
}
int main(){
int n,t;
scanf("%d%d",&n,&t);
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i].x,&a[i].y);
}
bool flag[1000010]={0};
for(int i=1;i<=n;i++){
for(int j=a[i].x;j<=a[i].y;j++){
flag[j]=1;
}
}
for(int i=1;i<=t;i++){
if(flag[i]!=1){
printf("-1");
return 0;
}
}
int ans=0;
sort(a,a+n,cmp);
int pos=0,now=0;
while(pos<n){
if(a[pos].x>=now){
now=a[pos].y;
ans++;
}
pos++;
}
printf("%d",ans);
return 0;
}