#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 6;
const long long INF = 1e15 + 7;
struct Node {
  Node *lc, *rc;
  long long tag;
  long long mx;
  Node() {
    lc = rc = NULL;
    mx = tag = 0;
  }
  void pull() { mx = max(lc->mx, rc->mx); }
};
Node* Build(int L, int R) {
  Node* node = new Node();
  if (L == R) {
    return node;
  }
  int mid = (L + R) >> 1;
  node->lc = Build(L, mid);
  node->rc = Build(mid + 1, R);
  return node;
}
void push(Node* node, int L, int R) {
  if (L == R) node->tag = 0;
  if (node->tag == 0) return;
  node->lc->tag += node->tag;
  node->rc->tag += node->tag;
  node->lc->mx += node->tag;
  node->rc->mx += node->tag;
  node->tag = 0;
  return;
}
void modify(Node* node, int L, int R, int l, int r, long long val) {
  if (L > r || l > R)
    return;
  else if (l <= L && R <= r) {
    node->mx += val;
    node->tag += val;
    return;
  }
  push(node, L, R);
  int mid = (L + R) >> 1;
  modify(node->lc, L, mid, l, r, val);
  modify(node->rc, mid + 1, R, l, r, val);
  node->pull();
  return;
}
long long query(Node* node, int L, int R, int l, int r) {
  if (l > R || L > r)
    return -INF;
  else if (l <= L && R <= r)
    return node->mx;
  push(node, L, R);
  int mid = (L + R) >> 1;
  return max(query(node->lc, L, mid, l, r), query(node->rc, mid + 1, R, l, r));
}
int L[MAX_N], R[MAX_N];
vector<int> v[MAX_N];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; n >= i; i++) {
    scanf("%d %d", &L[i], &R[i]);
    v[L[i]].push_back(R[i]);
  }
  Node* root = Build(1, m);
  int kirino = m;
  for (int i = 1; m >= i; i++) {
    modify(root, 1, m, i, i, -kirino);
    kirino--;
  }
  sort(v[m].begin(), v[m].end());
  long long ans = max(0, n - m);
  long long meruru = 0;
  for (int i = 0; m > i; i++) {
    if (i == 0) {
      for (int j : v[i]) {
        modify(root, 1, m, 1, j, 1);
      }
      ans = max(ans, query(root, 1, m, 1, m));
      continue;
    }
    modify(root, 1, m, i + 1, m, -1);
    meruru--;
    for (int j : v[i]) {
      modify(root, 1, m, i + 1, j, 1);
      if (j == m) meruru++;
    }
    ans = max(ans, query(root, 1, m, 1, m));
    ans = max(ans, meruru);
  }
  printf("%lld\n", ans);
}
