#include #include using namespace atcoder; using namespace std; using ll = long long; using mint = modint998244353; int main() { int N, M, A, B;cin >> N >> M >> A >> B; vector> G(N + 4); for (int i = 0;i < M;i++) { int l, r;cin >> l >> r; G[l].push_back(r + 1); G[r + 1].push_back(l); } for (int i = 1;i <= A;i++) G[N + 2].push_back(i); for (int i = B + 1;i <= N + 1;i++) G[i].push_back(N + 3); queue que;que.push(N + 2); vector dist(N + 4, -1); dist[N + 2] = 0; while (!que.empty()) { int v = que.front();que.pop(); for (auto nv : G[v]) { if (dist[nv] == -1) { dist[nv] = dist[v] + 1; que.push(nv); } } } cout << max(-1, dist[N + 3] - 2) << endl; }