//
// Created by liasa on 09/05/2026.
//
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll, ll>
#define v vector
#define lp(i, s, e) for (ll i = s; i < e; ++i)
bool comp(pll &a, pll &b) {
if (a.first == b.first)
return (a.second > b.second);
return (a.first < b.first);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n, m;
cin >> n >> m;
v<pll> V;
lp(i, 0, n) {
ll a, b;
cin >> a >> b;
if (a <= b) {
V.push_back({a, b});
V.push_back({a + m, b + m});
} else {
V.push_back({a, b + m});
V.push_back({a + m, b + 2 * m});
}
}
sort(V.begin(), V.end(), comp);
v<pll> p;
ll mx = 0;
for (auto &[a, b] : V) {
if (b > mx) {
p.push_back({a, b});
mx = b;
}
}
ll sz = p.size();
ll lg = 25;
v<v<ll>> bl(sz, v<ll>(lg));
ll poi = 0;
lp(i, 0, sz) {
while (poi < sz && p[poi].first <= p[i].second) {
poi++;
}
bl[i][0] = poi - 1;
}
lp(j, 1, lg) {
lp(i, 0, sz) { bl[i][j] = bl[bl[i][j - 1]][j - 1]; }
}
ll ans = 1e18;
lp(i, 0, sz) {
if (p[i].first >= m)
break;
ll st = p[i].first;
ll cur = i, cnt = 0;
for (ll j = lg - 1; j >= 0; --j) {
ll jump_to = bl[cur][j];
if (p[jump_to].second < st + m) {
cur = jump_to;
cnt += (1LL << j);
}
}
cur = bl[cur][0];
cnt++;
if (p[cur].second >= st + m) {
ans = min(ans, cnt+1);
}
}
if (ans == 1e18)
cout << -1;
else
cout << ans;
}