This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//-------------dilanyan------------\\ 
 
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
 
//------------------Kargpefines--------------------\\ 
 
#define ll long long
#define pb push_back
#define all(u) (u).begin(), (u).end()
#define pqueue priority_queue
#define upper upper_bound
#define lower lower_bound
#define umap unordered_map
#define uset unordered_set
 
void KarginSet(string name = "") {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    if (name.size()) {
        freopen((name + ".in").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
}
 
//-------------------KarginConstants------------------\\ 
 
const ll mod = 1e9 + 7;
const ll inf = 1e9;
 
//-------------------KarginCode-----------------------\\ 
 
const int N = 200005, M = 1000005;
int n, m;
pair<int, int> a[N];
int tree[4 * N], ind[4 * N];
struct segtree {
    int size = 1;
    void init() {
        while (size < n) size <<= 1;
        build(0, 0, size);
    }
    void build(int x, int lx, int rx) {
        if (rx - lx == 1) {
            if (lx < n) {
                tree[x] = a[lx].second;
                ind[x] = lx;
            }
            return;
        }
        int m = (lx + rx) / 2;
        build(2 * x + 1, lx, m);
        build(2 * x + 2, m, rx);
        if (tree[2 * x + 1] > tree[2 * x + 2]) tree[x] = tree[2 * x + 1], ind[x] = ind[2 * x + 1];
        else tree[x] = tree[2 * x + 2], ind[x] = ind[2 * x + 2];
    }
    pair<int,int> get(int l, int r, int x, int lx, int rx) {
        if (lx >= r || rx <= l) return make_pair(0, 0);
        if (lx >= l && rx <= r) return make_pair(tree[x], ind[x]);
        int m = (lx + rx) / 2;
        pair<int,int> getl = get(l, r, 2 * x + 1, lx, m),
            getr = get(l, r, 2 * x + 2, m, rx);
        if (getl.first > getr.first) return getl;
        return getr;
    }
    int get(int l, int r) {
        return get(l, r, 0, 0, size).second;
    }
};
int up[N][20];
void KarginSolve() {
    cin >> n >> m;
    for (int i = 0;i < n;i++) {
        cin >> a[i].first >> a[i].second;
        if (a[i].first > a[i].second) a[i].second += m;
    }
    sort(a, a + n);
    segtree st;
    st.init();
    for (int i = 0; i < n;i++) {
        int l = i, r = n - 1, indx = n;
        while (l <= r) {
            int mid = (l + r) / 2;
            if (a[mid].first > a[i].second) {
                r = mid - 1;
                indx = mid;
            }
            else l = mid + 1;
        }
        up[i][0] = st.get(i, indx);
    }
    for (int j = 1;j < 20;j++) {
        for (int i = 0;i < n;i++) {
            up[i][j] = up[up[i][j - 1]][j - 1];
        }
    }
    int ans = inf;
    for (int i = 0;i < n;i++) {
        int l = 0, r = n - 1, res = inf;
        while (l <= r) {
            int mid = (l + r) / 2;
            int x = i;
            for (int j = 0;j < 20;j++) {
                if (mid & (1 << j)) x = up[x][j];
            }
            if (a[x].second >= a[i].first + m) {
                r = mid - 1;
                res = mid;
            }
            else {
                l = mid + 1;
            }
        }
        ans = min(ans, res + 1);
    }
    if (ans == inf) cout << -1 << '\n';
    else cout << ans << '\n';
}
int main() {
    KarginSet();
    int test = 1;
    //cin >> test;
    while (test--) {
        KarginSolve();
    }
    return 0;
}
Compilation message (stderr)
Main.cpp:1:1: warning: multi-line comment [-Wcomment]
    1 | //-------------dilanyan------------\\
      | ^
Main.cpp:8:1: warning: multi-line comment [-Wcomment]
    8 | //------------------Kargpefines--------------------\\
      | ^
Main.cpp:27:1: warning: multi-line comment [-Wcomment]
   27 | //-------------------KarginConstants------------------\\
      | ^
Main.cpp:32:1: warning: multi-line comment [-Wcomment]
   32 | //-------------------KarginCode-----------------------\\
      | ^
Main.cpp: In function 'void KarginSet(std::string)':
Main.cpp:22:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |         freopen((name + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |         freopen((name + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |