rt
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(qw,we,er) for(int qw=we;qw<=er;++qw)
int n,t,t1,t2,t3,ans;
struct xy
{
int x,y;
}a[25000];
struct cmp
{
bool operator ()(xy qwer,xy wert)
{
return qwer.x<wert.x;
}
};
signed main()
{
scanf("%lld%lld",&n,&t);
FOR(i,1,n)
{
scanf("%lld%lld",&a[i].x,&a[i].y);
}
sort(a+1,a+1+n,cmp());
t1=1;
FOR(i,1,t)
{
if(a[t1].x>i)
{
printf("-1\n");
exit(0);
}
t2=0;
while(a[t1].x<=i&&t1<=n)t2=max(t2,a[t1++].y);
if(t2<t1)
{
printf("-1\n");
exit(0);
}
ans++;
i=t2+1;
}
printf("%lld\n",ans);
return 0;
}