#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000002;
const int mod = 1000000007;
vector<int> lis[200010];
int Max[200010 << 2], tag[200010 << 2];
void Build(int x, int l, int r) {
  if (l == r) {
    Max[x] = l;
    tag[x] = 0;
    return;
  }
  int mid = (l + r) >> 1;
  Build(x << 1, l, mid);
  Build(x << 1 | 1, mid + 1, r);
  Max[x] = max(Max[x << 1], Max[x << 1 | 1]);
}
void PushDown(int x) {
  if (tag[x] == 0) return;
  Max[x << 1] += tag[x];
  tag[x << 1] += tag[x];
  Max[x << 1 | 1] += tag[x];
  tag[x << 1 | 1] += tag[x];
  tag[x] = 0;
}
void Modify(int x, int L, int R, int l, int r) {
  if (r < L || l > R) return;
  if (l <= L && r >= R) {
    ++tag[x];
    ++Max[x];
    return;
  }
  PushDown(x);
  int Mid = (L + R) >> 1;
  Modify(x << 1, L, Mid, l, r);
  Modify(x << 1 | 1, Mid + 1, R, l, r);
  Max[x] = max(Max[x << 1], Max[x << 1 | 1]);
}
int Query(int x, int L, int R, int l, int r) {
  if (r < L || l > R) return -INF;
  if (l <= L && r >= R) return Max[x];
  PushDown(x);
  int Mid = (L + R) >> 1;
  return max(Query(x << 1, L, Mid, l, r), Query(x << 1 | 1, Mid + 1, R, l, r));
}
int main() {
  int n, m, x, y, ans;
  scanf("%d %d", &n, &m);
  ans = max(0, n - m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", &x, &y);
    lis[x].push_back(y);
  }
  Build(1, 0, m + 1);
  for (int i = 0; i <= 3; ++i) {
    for (int j = 0; j < lis[i].size(); ++j) Modify(1, 0, m + 1, 0, lis[i][j]);
    ans = max(ans, Query(1, 0, m + 1, i + 1, m + 1) - i - m - 1);
  }
  printf("%d\n", ans);
  return 0;
}
