//chockolateman
#include<bits/stdc++.h>
using namespace std;
int N,M,s[200005],e[200005];
long long up[200005][21];
int nxt[200005];
map<int,int> my_pos;
int calc(int i)
{
int cur;
if(s[i] <= e[i])
cur = (e[i] - s[i] + 1);
else
cur = (e[i] + M - s[i]);
if(cur + up[i][20] < M)
return N+1;
if(cur==M)
return 0;
int pos = i;
int ret = 1;
for(int L = 20 ; L >= 0 ; L--)
while(cur + up[pos][L] < M)
{
cur += up[pos][L];
pos = my_pos[(e[pos] + up[pos][L])%M];
ret += (1<<L);
}
ret++;
return ret;
}
int main()
{
scanf("%d%d",&N,&M);
priority_queue<pair<int,pair<int,int>>> events;
for(int i = 1 ; i <= N ; i++)
{
scanf("%d%d",&s[i],&e[i]);
my_pos[e[i]] = i;
if(s[i] <= e[i])
{
events.push({-s[i],{1,i}});
events.push({-e[i],{-1,i}});
events.push({-(s[i]+M),{1,i}});
events.push({-(e[i]+M),{-1,i}});
nxt[i] = e[i];
}
else
{
events.push({0,{1,i}});
events.push({-e[i],{-1,i}});
events.push({-s[i],{1,i}});
events.push({-(e[i]+M),{-1,i}});
nxt[i] = e[i]+M;
}
}
multiset<int> active;
while(!events.empty())
{
pair<int,pair<int,int>> cur = events.top();
events.pop();
int t = -cur.first;
int op = cur.second.first;
int i = cur.second.second;
if(op==1)
{
if(s[i] <= e[i])
{
if(t < M)
active.insert(e[i]);
else
active.insert(e[i]+M);
}
else
{
if(t==0)
active.insert(e[i]);
else
active.insert(e[i]+M);
}
}
else
{
if(s[i] <= e[i])
{
if(t < M)
nxt[i] = max(nxt[i],*active.rbegin());
}
else
{
nxt[i] = max(nxt[i],*active.rbegin());
}
active.erase(active.find(t));
}
}
for(int i = 1 ; i <= N ; i++)
{
if(s[i] <= e[i])
up[i][0] = nxt[i] - e[i];
else
up[i][0] = nxt[i]%M - e[i];
}
for(int L = 1 ; L <= 20 ; L++)
for(int i = 1 ; i <= N ; i++)
{
up[i][L] = up[i][L-1] + up[my_pos[(e[i]+up[i][L-1])%M]][L-1];
}
int ans = N+1;
for(int i = 1 ; i <= N ; i++)
ans = min(ans,calc(i));
if(ans==N+1)
ans = -1;
printf("%d\n",ans);
return 0;
}