#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e5 + 5;
int n, m, lazy[1 << 19], pos[1 << 19];
pair<int, int> a[N];
struct segtree {
 private:
  int tree[1 << 19], arr[N], type;
  void lazy_update(int k, int l, int r) {
    if (lazy[k] == 0) {
      return;
    }
    tree[k] += lazy[k];
    if (l != r) {
      lazy[k << 1] += lazy[k];
      lazy[k << 1 | 1] += lazy[k];
    }
    lazy[k] = 0;
  }
  void merge_tree(int k, int l, int r) {
    tree[k] = min(tree[l], tree[r]);
    if (type) {
      if (tree[k] == tree[l]) {
        pos[k] = pos[l];
      } else {
        pos[k] = pos[r];
      }
    }
  }
  void build(int k, int l, int r) {
    if (l > r) {
      return;
    }
    if (l == r) {
      tree[k] = arr[l];
      if (type) {
        pos[k] = l;
      }
      return;
    }
    int mid = (l + r) >> 1;
    build(k << 1, l, mid);
    build(k << 1 | 1, mid + 1, r);
    merge_tree(k, k << 1, k << 1 | 1);
  }

 public:
  void init(int mode) {
    type = mode;
    if (!mode) {
      for (int i = m + 1; i >= 1; --i) {
        arr[i] = m - i + 1;
      }
      build(1, 1, m + 1);
    } else {
      for (int i = 1; i <= n; ++i) {
        arr[i] = m - a[i].second;
      }
      build(1, 1, n);
    }
  }
  void update(int k, int l, int r, int L, int R, int val) {
    if (!type) {
      lazy_update(k, l, r);
    }
    if (l > r || l > R || r < L) return;
    if (L <= l && r <= R) {
      lazy[k] += val;
      lazy_update(k, l, r);
      return;
    }
    int mid = (l + r) >> 1;
    update(k << 1, l, mid, L, R, val);
    update(k << 1 | 1, mid + 1, r, L, R, val);
    merge_tree(k, k << 1, k << 1 | 1);
  }
  void remove(int k, int l, int r, int p) {
    if (l > r || l > p || r < p) return;
    if (l == r && l == p) {
      tree[k] = 1e9 + 7;
      return;
    }
    int mid = (l + r) >> 1;
    remove(k << 1, l, mid, p);
    remove(k << 1 | 1, mid + 1, r, p);
    merge_tree(k, k << 1, k << 1 | 1);
  }
  int query1(int k, int l, int r, int L, int R) {
    if (l > r || l > R || r < L) return m + 5;
    if (!type) {
      lazy_update(k, l, r);
    }
    if (L <= l && r <= R) {
      return tree[k];
    }
    int mid = (l + r) >> 1;
    int lf = query1(k << 1, l, mid, L, R);
    int rt = query1(k << 1 | 1, mid + 1, r, L, R);
    return min(lf, rt);
  }
  pair<int, int> query2(int k, int l, int r, int L, int R) {
    if (l > r || l > R || r < L) return pair<int, int>(1e9 + 7, 0);
    if (L <= l && r <= R) {
      return pair<int, int>(tree[k], pos[k]);
    }
    int mid = (l + r) >> 1;
    pair<int, int> lf = query2(k << 1, l, mid, L, R);
    pair<int, int> rt = query2(k << 1 | 1, mid + 1, r, L, R);
    pair<int, int> re = lf;
    if (re.first > rt.first) {
      re.first = rt.first;
      re.second = rt.second;
    }
    return re;
  }
} tree1, tree2;
vector<int> g[N];
bool vis[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &a[i].first, &a[i].second);
  }
  tree1.init(0);
  sort(a + 1, a + n + 1);
  tree2.init(1);
  for (int i = 1; i <= n; ++i) {
    g[a[i].second].push_back(i);
    tree1.update(1, 1, m + 1, 1, a[i].second, -1);
  }
  int cnt = 0, ans = n;
  for (int i = 1; i <= m; ++i) {
    for (const int &v : g[i]) {
      cnt += (!vis[v]);
    }
    tree1.update(1, 1, m + 1, i + 1, i + 1, -cnt);
    int idx = lower_bound(a + 1, a + n + 1, pair<int, int>(i, 0)) - a;
    pair<int, int> tmp = tree2.query2(1, 1, n, idx, n);
    tree1.update(1, 1, m + 1, 1, m - tmp.first, 1);
    tree2.remove(1, 1, n, tmp.second);
    vis[tmp.second] = true;
    int tmp_val = tree1.query1(1, 1, m + 1, i + 1, m + 1);
    if (tmp_val < 0) {
      tmp_val = -tmp_val;
    } else {
      tmp_val = 0;
    }
    ans = min(ans, tmp_val);
    tree1.update(1, 1, m + 1, i + 1, i + 1, cnt);
  }
  printf("%d", ans);
  return 0;
}
