#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
void solve(){
int n,m;
cin>>n>>m;
vector<pair<int,int>> v(n);
for(auto &[a,b] : v){
cin>>a>>b;
if(a > b) b += m;
}
sort(v.begin(),v.end());
int ans = 1e9;
for(int i=0;i<n;i++){
vector<pair<int,int>> tmp = v;
int l = v[i].first , r = v[i].second;
int tar = v[i].first + m - 1;
int cnt = 1;
for(auto &[x,y] : v){
if(x < l) x += m , y += m;
}
while(r < tar){
int mx = 0;
for(auto [x,y] : v){
if(l <= x && x <= r+1){
mx = max(mx , y);
}
}
if(mx <= r) {
v = tmp;
goto gt;
}
cnt ++;
r = max(mx , r);
}
ans = min(ans , cnt);
v = tmp;
gt:continue;
}
cout<<(ans == 1e9 ? -1 : ans)<<endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}