#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct edge {
  int nxt, to;
} e[N];
int fir[N], a[N];
namespace Segment_Tree {
int key[N << 1], lazy[N << 1];
inline int ID(int L, int R) { return (L + R) | (L != R); }
inline void PushDown(int L, int R) {
  if (!lazy[ID(L, R)] || L == R) return;
  lazy[ID(L, ((L + R) >> 1))] += lazy[ID(L, R)];
  lazy[ID(((L + R) >> 1) + 1, R)] += lazy[ID(L, R)];
  key[ID(L, ((L + R) >> 1))] += lazy[ID(L, R)];
  key[ID(((L + R) >> 1) + 1, R)] += lazy[ID(L, R)];
  lazy[ID(L, R)] = 0;
}
inline void PushUp(int L, int R) {
  key[ID(L, R)] =
      max(key[ID(L, ((L + R) >> 1))], key[ID(((L + R) >> 1) + 1, R)]);
}
inline void Init(int L, int R) {
  lazy[ID(L, R)] = 0;
  if (L == R) {
    key[ID(L, R)] = L;
    return;
  }
  Init(L, ((L + R) >> 1));
  Init(((L + R) >> 1) + 1, R);
  PushUp(L, R);
}
inline void Add(int L, int R, int l, int r) {
  if (L == l && R == r) {
    key[ID(L, R)]++;
    lazy[ID(L, R)]++;
    return;
  }
  PushDown(L, R);
  if (r <= ((L + R) >> 1))
    Add(L, ((L + R) >> 1), l, r);
  else if (l > ((L + R) >> 1))
    Add(((L + R) >> 1) + 1, R, l, r);
  else {
    Add(L, ((L + R) >> 1), l, ((L + R) >> 1));
    Add(((L + R) >> 1) + 1, R, ((L + R) >> 1) + 1, r);
  }
  PushUp(L, R);
}
inline int Max(int L, int R, int l, int r) {
  if (L == l && R == r) return key[ID(L, R)];
  PushDown(L, R);
  if (r <= ((L + R) >> 1))
    return Max(L, ((L + R) >> 1), l, r);
  else if (l > ((L + R) >> 1))
    return Max(((L + R) >> 1) + 1, r, l, r);
  else
    return max(Max(L, ((L + R) >> 1), l, ((L + R) >> 1)),
               Max(((L + R) >> 1) + 1, R, ((L + R) >> 1) + 1, r));
}
}  // namespace Segment_Tree
using namespace Segment_Tree;
int main() {
  int ans = 0, n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1, l, r; i <= n; i++) {
    scanf("%d%d", &l, &r);
    e[i] = (edge){fir[l], r};
    fir[l] = i;
  }
  Segment_Tree::Init(0, m + 1);
  for (int i = 0; i <= m; i++) {
    for (int j = fir[i]; j; j = e[j].nxt)
      Segment_Tree::Add(0, m + 1, 0, e[j].to);
    ans = max(ans, Segment_Tree::Max(0, m + 1, i + 1, m + 1) - m - i - 1);
  }
  printf("%d\n", ans);
  return 0;
}
