description: A group of n alpinists has just reached the foot of the mountain. The initial difficulty of climbing this mountain can be described as an integer d.
  
  Each alpinist can be described by two integers s and a, where s is his skill of climbing mountains and a is his neatness.
  
  An alpinist of skill level s is able to climb a mountain of difficulty p only if p ≤ s. As an alpinist climbs a mountain, they affect the path and thus may change mountain difficulty. Specifically, if an alpinist of neatness a climbs a mountain of difficulty p the difficulty of this mountain becomes max(p, a). 
  
  Alpinists will climb the mountain one by one. And before the start, they wonder, what is the maximum number of alpinists who will be able to climb the mountain if they choose the right order. As you are the only person in the group who does programming, you are to answer the question.
  
  Note that after the order is chosen, each alpinist who can climb the mountain, must climb the mountain at that time. 
  
  Input
  
  The first line contains two integers n and d (1 ≤ n ≤ 500 000; 0 ≤ d ≤ 10^9) — the number of alpinists and the initial difficulty of the mountain.
  
  Each of the next n lines contains two integers s_i and a_i (0 ≤ s_i, a_i ≤ 10^9) that define the skill of climbing and the neatness of the i-th alpinist.
  
  Output
  
  Print one integer equal to the maximum number of alpinists who can climb the mountain if they choose the right order to do so.
  
  Examples
  
  Input
  
  
  3 2
  2 6
  3 5
  5 7
  
  
  Output
  
  
  2
  
  
  Input
  
  
  3 3
  2 4
  6 4
  4 6
  
  
  Output
  
  
  2
  
  
  Input
  
  
  5 0
  1 5
  4 8
  2 7
  7 6
  3 2
  
  
  Output
  
  
  3
  
  Note
  
  In the first example, alpinists 2 and 3 can climb the mountain if they go in this order. There is no other way to achieve the answer of 2.
  
  In the second example, alpinist 1 is not able to climb because of the initial difficulty of the mountain, while alpinists 2 and 3 can go up in any order.
  
  In the third example, the mountain can be climbed by alpinists 5, 3 and 4 in this particular order. There is no other way to achieve optimal answer.
solution: #include <bits/stdc++.h>
  using namespace std;
  using ll = long long;
  using pii = pair<int, int>;
  constexpr int INF = 0x3f3f3f3f;
  class SegmentTree {
   public:
    using Node = vector<int>;
    explicit SegmentTree(const vector<int>& vec)
        : tree(vec.size() << 2), size((int)vec.size()) {
      Build(0, 0, size - 1, vec);
    }
    int Get(int l, int r, int x) {
      return QueryRangeImpl(0, 0, size - 1, l, r, x);
    }
  
   private:
    int Left(int v) const { return (v << 1) + 1; }
    int Right(int v) const { return (v << 1) + 2; }
    void UpdateVertex(int v) {
      merge((tree[Left(v)]).begin(), (tree[Left(v)]).end(),
            (tree[Right(v)]).begin(), (tree[Right(v)]).end(),
            back_inserter(tree[v]));
    }
    void Build(int v, int tl, int tr, const vector<int>& vec) {
      if (tl == tr) {
        tree[v].push_back(vec[tl]);
        return;
      }
      const int mid = (tl + tr) >> 1;
      Build(Left(v), tl, mid, vec);
      Build(Right(v), mid + 1, tr, vec);
      UpdateVertex(v);
    }
    int QueryRangeImpl(int v, int tl, int tr, int l, int r, int x) {
      if (l > r) {
        return 0;
      }
      if (tl == l && r == tr) {
        return (int)distance(lower_bound((tree[v]).begin(), (tree[v]).end(), x),
                             end(tree[v]));
      }
      const int mid = (tl + tr) >> 1;
      return QueryRangeImpl(Left(v), tl, mid, l, min(mid, r), x) +
             QueryRangeImpl(Right(v), mid + 1, tr, max(l, mid + 1), r, x);
    }
  
   private:
    vector<Node> tree;
    const int size;
  };
  void SolveTask() {
    int n, d;
    cin >> n >> d;
    vector<pii> arr;
    arr.reserve(n);
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      int a, b;
      cin >> a >> b;
      if (a < d) {
        continue;
      }
      if (b <= d) {
        ++ans;
      } else {
        arr.emplace_back(a, b);
      }
    }
    arr.emplace_back(-1, d);
    sort((arr).begin(), (arr).end(),
         [](pii a, pii b) { return a.second < b.second; });
    vector<int> dp(arr.size(), 0);
    vector<int> vec(arr.size());
    for (int i = 0; i < (int)arr.size(); ++i) {
      vec[i] = arr[i].first;
    }
    SegmentTree st(vec);
    for (int i = 1; i < (int)arr.size(); ++i) {
      int l = 0;
      int r = i - 1;
      while (l <= r) {
        int mid = (l + r) >> 1;
        if (arr[mid].second <= arr[i].first) {
          l = mid + 1;
        } else {
          r = mid - 1;
        }
      }
      const int j = l - 1;
      dp[i] = max(dp[i - 1], dp[j] + 1 + st.Get(j + 1, i - 1, arr[i].second));
    }
    ans += dp.back();
    cout << ans << "\n";
  }
  int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int test_cases = 1;
    for (int yt = 1; yt <= test_cases; ++yt) {
      SolveTask();
    }
    return 0;
  }
  