#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200005;
vector<int> vec[MAX_N];
int N, M, mx[MAX_N << 2], tag[MAX_N << 2];
void build(int x, int l, int r) {
  if (l == r) {
    mx[x] = -M + l - 1;
    return;
  }
  int mid = (l + r) >> 1;
  build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r);
  mx[x] = max(mx[x << 1], mx[x << 1 | 1]);
}
void push_down(int x) {
  if (tag[x]) {
    mx[x << 1] += tag[x], tag[x << 1] += tag[x];
    mx[x << 1 | 1] += tag[x], tag[x << 1 | 1] += tag[x];
    tag[x] = 0;
  }
}
void update(int x, int l, int r, int L, int R) {
  if (L <= l && r <= R) {
    mx[x]++, tag[x]++;
    return;
  }
  push_down(x);
  int mid = (l + r) >> 1;
  if (mid >= L) update(x << 1, l, mid, L, R);
  if (mid < R) update(x << 1 | 1, mid + 1, r, L, R);
  mx[x] = max(mx[x << 1], mx[x << 1 | 1]);
}
int query(int x, int l, int r, int L, int R) {
  if (L <= l && r <= R) return mx[x];
  push_down(x);
  int mid = (l + r) >> 1, res = -100000000;
  if (mid >= L) res = query(x << 1, l, mid, L, R);
  if (mid < R) res = max(res, query(x << 1 | 1, mid + 1, r, L, R));
  return res;
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 1, L, R; i <= N; ++i) {
    scanf("%d%d", &L, &R);
    vec[L].push_back(R);
  }
  build(1, 1, M + 1);
  int res = N - M;
  for (int i = 0; i < M - 1; ++i) {
    for (int x : vec[i]) update(1, 1, M + 1, 1, x);
    res = max(res, query(1, 1, M + 1, i + 2, M + 1) - i);
  }
  printf("%d\n", res);
  return 0;
}
