#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;
const int MAX_V = 200010;
ll INF = 100000000000000;
struct edge{ll to,cap,rev;};
vector<edge> G[MAX_V];
bool used[MAX_V];
ll b[MAX_V],f[MAX_V],can[MAX_V][MAX_V];
vector<int> v,w;
void add_edge(ll from,ll to,ll cap){
    G[from].push_back({to,cap,(ll)G[to].size()});
    G[to].push_back({from,0,(ll)G[from].size() - 1});
}

ll dfs(int s,int t,ll val){
    for(edge e:G[s]){
        if(used[e.to]) continue;
        b[e.to] += e.cap;
        if(b[e.to]>=val){
            used[e.to] = true;
            v.push_back(e.to); w.push_back(e.to);
        }
    }
    if(v.size()==0) return 0;
    int vv = v.back();
    if(vv==t) return 1;
    v.pop_back();
    return dfs(vv,t,val);
}

ll rev_dfs(int s,int t,ll val){
    for(int i=0;i<MAX_V;i++) f[i] = 0;
    f[t] = val;
    while(w.size()){
        int now = w.back();
        w.pop_back();
        used[now] = false;
        for(edge ee:G[now]){
            int from = ee.to;
            if(!used[from]) continue;
            int j = can[from][now];
            if(j==-1) continue;
            edge &e = G[from][j];
            ll f_ = min(f[now],e.cap);
            e.cap -= f_;
            G[e.to][e.rev].cap += f_;
            f[now] -= f_; f[from] += f_;
        }
    }
}

ll max_flow(int s,int t){
    ll flow = 0,val = 0;
    int i,j;
    for(i=0;i<MAX_V;i++){
        b[i] = 0;
        for(j=0;j<MAX_V;j++){
            can[i][j]= -1;
        }
        for(j=0;j<G[i].size();j++){
            can[i][G[i][j].to] = j;
            val = max(val,G[i][j].cap);
        }
    }
    while(val){
        v.clear(); w.clear(); w.push_back(s);
        for(i=0;i<MAX_V;i++){
            b[i] = 0; used[i] = false;
        }
        used[s] = true;
        if(!dfs(s,t,val)){
            val /= 2;
            continue;
        }
        rev_dfs(s,t,val);
        flow += val;
    }
    return flow;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int s = n + 3 * m, t = s + 1;
  for (int i = 0; i < n; ++i) {
    int l, r;
    cin >> l >> r;
    --r;
    add_edge(s, i, 1);
    if (l) {
      add_edge(i, n + (l - 1), 1);
    }
    if (r < m) {
      add_edge(i, n + m + r, 1);
    }
  }
  for (int j = 0; j < m; ++j) {
    add_edge(n + j, n + 2 * m + j, 1);
    add_edge(n + m + j, n + 2 * m + j, 1);
    add_edge(n + 2 * m + j, t, 1);
    if (j) {
      add_edge(n + j, n + (j - 1), INF);
    }
    if (j < m - 1) {
      add_edge(n + m + j, n + m + (j + 1), INF);
    }
  }
  cout << n - max_flow(s, t) << endl;
}
