WA on #11#15
#include<iostream>
#include<cstdio>
#include<algorithm>
#define int long long
using namespace std;
const int N=3e5+5;
struct Node
{
int s,e;
};
Node a[N];
int cmp(Node a1,Node a2)
{
if(a1.s==a2.s)
{
return a1.e>a2.e;
}
return a1.s<a2.s;
}
signed main(){
//freopen("cleaning.in","r",stdin);
//freopen("kkksc.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,t;
cin >> n >> t;
for(int i=1;i<=n;i++)
{
cin >> a[i].s >> a[i].e;
}
sort(a+1,a+n+1,cmp);
if(a[1].s>1)
{
cout << -1 << endl;
return 0;
}
int r=a[1].e;
int ans=1;
for(int i=2;i<=n;i++)
{
int kkk=-1;
for(int j=i;j<=n;j++)
{
if(a[j].s>r+1)
{
break;
}
kkk=max(kkk,a[j].e);
i=j;
}
if(kkk<=r)
{
cout << -1;
return 0;
}
r=kkk;
ans++;
if(r>=t)
{
break;
}
}
if(r<t)
{
cout << -1;
return 0;
}
cout << ans << endl;
return 0;
}