#include <bits/stdc++.h>
#define maxn 2000005
using namespace std;
inline int read() {
int x = 0, f = 1; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
int n, m, k;
struct edge {
int v, w, nxt;
}e[maxn * 5];
int head[maxn], cnt;
void add(int u, int v, int w) {
e[++cnt] = { v, w, head[u] };
head[u] = cnt;
}
int dis[maxn], vis[maxn];
void spfa() {
memset(dis, 0x3f, sizeof(dis));
dis[1] = 0;
queue<int> q;
q.push(1);
vis[1] = 1;
while (!q.empty()) {
int x = q.front(); q.pop();
vis[x] = 0;
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].v, z = e[i].w;
if (dis[y] > dis[x] + z) {
dis[y] = dis[x] + z;
if (!vis[y]) q.push(y), vis[y] = 1;
}
}
}
}
signed main() {
n = read(), m = read();
for (int i = 1; i <= n; i++) {
int s = read(), t = read();
add(m + s, m + t, 1);
add(s, m + s, 0), add(m + t, t, 0);
}
for (int i = 1; i < m; i++) add(i + 1, i, 0);
spfa();
cout << dis[m];
}