#include <bits/stdc++.h>
using namespace std;
int fn[800002], n, m;
pair<int, int> p[200000];
inline int gt(int i) {
  i = m + 2 - i;
  int an = 0;
  while (i) an += fn[i], i ^= i & -i;
  return an;
}
inline void ad(int i) {
  i = m + 2 - i;
  while (i <= m + 1) ++fn[i], i += i & -i;
}
bool cmp(pair<int, int> const &a, pair<int, int> const &b) {
  if (a.first == b.first) return a.second > b.second;
  return a.first < b.first;
}
void ad(int i, int z, int l = 0, int r = m + 1, int id = 1) {
  if (l + 1 == r) {
    fn[i] += z;
    return;
  }
  int m = l + r >> 1, a = id << 1, b = id << 1 | 1;
  if (i < m)
    ad(i, z, l, m, a);
  else
    ad(i, z, m, r, b);
  fn[id] = max(fn[a], fn[b]);
}
bool ok(int k) {
  for (int i = 0; i < (int)(m + 2); ++i) ad(i, -(m + 1) + i);
  for (int i = 0; i < (int)(n); ++i) {
    ad(p[i].second, 1);
    int hi = fn[1], ch = p[i].first + m + 1 - p[i].second + k;
    if (hi + p[i].first > 4) return false;
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (int)(n); ++i) scanf("%d%d", &p[i].first, &p[i].second);
  sort(p, p + n, cmp);
  int l = 0, r = n;
  while (r > l) {
    int m = l + r >> 1;
    if (ok(m))
      r = m;
    else
      l = m + 1;
  }
  printf("%d\n", l);
}
