#include <bits/stdc++.h>
using namespace std;
void _dbg(string) { cout << endl; }
template <class H, class... T>
void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream& operator<<(ostream& o, const pair<T, U>& p) {
  o << "(" << p.first << "," << p.second << ")";
  return o;
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> vec(4 * 100010, vector<int>());
  int ans = 0;
  for (int i = (int)(0); i < (int)(n); i++) {
    int a, b;
    cin >> a >> b;
    if (a == 0 && b == m + 1) {
      ans++;
      continue;
    } else {
      vec[b].push_back((a + m));
    }
  }
  priority_queue<int, vector<int>, greater<int>> pq;
  vector<bool> used(m, false);
  for (int i = (int)(1); i < (int)(2 * m + 5); i++) {
    for (auto j : vec[i]) pq.push(j);
    if (!used[i % m] && !pq.empty()) pq.pop(), used[i % m] = true;
    while (!pq.empty() && pq.top() <= i) {
      ans++;
      pq.pop();
    }
  }
  cout << ans << endl;
  return 0;
}
