#include using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) #define rep2(i, x, n) for(int i = x; i <= n; i++) #define rep3(i, x, n) for(int i = x; i >= n; i--) #define elif else if #define sp(x) fixed << setprecision(x) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() using ll = long long; using pii = pair; using pil = pair; using pli = pair; using pll = pair; const int MOD = 1000000007; //const int MOD = 998244353; const int inf = (1<<30)-1; const ll INF = (1LL<<60)-1; const double pi = acos(-1.0); const double EPS = 1e-10; template bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;}; template bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;}; template struct Weighted_Graph{ struct edge{ int to; T cost; edge(int to, T cost) : to(to), cost(cost) {} }; vector> es; const T INF_T; const int n; Weighted_Graph(int n) : INF_T(numeric_limits::max()), n(n){ es.resize(n); } void add_edge(int from, int to, T cost, bool directed = false){ es[from].eb(to, cost); if(!directed) es[to].eb(from, cost); } T dijkstra(int s, int t){ vector d(n, INF_T); using P = pair; priority_queue, greater

> que; que.emplace(d[s] = 0, s); while(!que.empty()){ T p; int i; tie(p, i) = que.top(); que.pop(); if(p > d[i]) continue; for(auto &e: es[i]){ if(chmin(d[e.to], d[i]+e.cost)) que.emplace(d[e.to], e.to); } } return (d[t] == INF_T? -1 : d[t]); } }; int main(){ int N, M, A, B; cin >> N >> M >> A >> B; Weighted_Graph G(N+3); int s = N+1, t = N+2; rep2(i, 0, A-1) G.add_edge(s, i, 0, true); rep2(i, B, N) G.add_edge(i, t, 0, true); rep(i, M){ int l, r; cin >> l >> r; l--; G.add_edge(l, r, 1); } cout << G.dijkstra(s, t) << endl; }