//
// Created by liasa on 09/05/2026.
//
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll, ll>
#define v vector
#define lp(i, s, e) for (ll i = s; i < e; ++i)
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n, m;
cin >> n >> m;
v<pll> V;
ll st = 1e18;
lp(i, 0, n) {
ll a, b;
cin >> a >> b;
if (a <= b) {
V.push_back({a, b});
} else
st = min(st, a);
}
if (st == 1e18) {
cout << -1;
} else {
sort(V.begin(), V.end());
ll ed = 0, mx = 0, ans = 0;
for (auto [a, b] : V) {
if (a > ed) {
if (mx <= ed) {
ans = -1;
break;
}
ed = mx;
ans++;
}
mx = max(mx, b);
if (ed >= st)
break;
}
if (ans != -1 && ed < st) {
if (mx >= st) {
ans++;
} else {
ans = -1;
}
}
}
}