#include <bits/stdc++.h>
using namespace std;
pair<int, int> operator-(const pair<int, int> &l, const pair<int, int> &r) {
  return pair<int, int>(l.first - r.first, l.second - r.second);
}
int operator/(const pair<int, int> &l, const pair<int, int> &r) {
  return l.first * r.second - l.second * r.first;
}
int sign(pair<int, int> x) { return pair<int, int>(0, 0) < x ? 1 : -1; }
double atan2(pair<int, int> x) { return atan2(x.second, x.first); }
const int MX = 1 << 18;
const int MM = 1000000007;
vector<int> G[MX];
int a, b;
struct node {
  node(int v, int mx) : v(v), mx(mx) {}
  node() {}
  int v, mx;
};
struct Tree {
  node t[MX * 2];
  node merge(node l, node r) { return node(l.v + r.v, max(l.v + r.mx, l.mx)); }
  void update(int x, int v) {
    x += MX;
    t[x].v += v;
    t[x].mx = t[x].v;
    while (x > 1) {
      x /= 2;
      t[x] = merge(t[x * 2], t[x * 2 + 1]);
    }
  }
  int read(int v) {
    v += MX;
    node r = node(0, 0);
    while (v & (v + 1)) {
      if (v & 1) r = merge(r, t[v++]);
      v /= 2;
    }
    return r.mx;
  }
  int read() { return t[1].mx; }
} tree;
int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; i++) {
    scanf("%d%d", &a, &b);
    G[a].push_back(b);
  }
  int mx = max(0, M - N), cnt = 0;
  for (int i = 0; i <= M + 1; i++) tree.update(i, 1);
  for (int i = 0; i <= M; i++) {
    tree.update(i, -1);
    for (int c : G[i]) {
      cnt++;
      tree.update(c + 1, -1);
    }
    mx = max(mx, tree.read(i + 1) + cnt - M - 1);
  }
  printf("%d\n", mx);
}
