#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
ll n, m; cin >> n >> m;
map<ll, ll> tmp;
vector<pair<ll, ll>> mp;
for (int i = 0; i < n; i++) {
ll s, e; cin >> s >> e;
if (s > e) e += m;
tmp[s] = max(tmp[s], e);
tmp[s+m] = max(tmp[s+m], e+m);
}
ll mxr = 0;
for (auto [l, r] : tmp) if (r > mxr) mxr = r, mp.push_back({l, r});
n = mp.size();
vector<vector<int>> jump(20, vector<int>(n, -1));
for (int i = 0; i < n; i++) {
auto [l, r] = mp[i];
int nx = lower_bound(mp.begin(), mp.end(), pair<ll, ll>{r+1, 0})-mp.begin()-1;
if (nx <= i) continue;
jump[0][i] = nx;
}
for (int b = 1; b < 20; b++) for (int i = 0; i < n; i++) if (jump[b-1][i] != -1) jump[b][i] = jump[b-1][jump[b-1][i]];
int ans = 1e9;
auto check = [&](int i, int j) -> bool {
return mp[j].second >= mp[i].first+m;
};
for (int i = 0; i < n; i++) {
int j = i, cost = 2;
for (int b = 19; b >= 0; b--) {
int nxt = jump[b][j];
if (nxt != -1 && !check(i, nxt)) j = nxt, cost += 1<<b;
}
if (jump[0][j] != -1 && check(i, jump[0][j])) ans = min(ans, cost);
}
cout << (ans == 1e9 ? -1 : ans) << endl;
}