/** * @FileName a.cpp * @Author kanpurin * @Created 2020.10.02 22:52:45 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; template struct Dijkstra { private: int V; struct edge { int to; T cost; }; vector> G; public: const T inf = numeric_limits::max(); vector d; Dijkstra(int V) : V(V) { G.resize(V); } void add_edge(int from, int to, T weight, bool directed = false) { G[from].push_back({to,weight}); if (!directed) G[to].push_back({from,weight}); } void build(int s) { d.assign(V, inf); typedef tuple P; priority_queue, greater

> pq; d[s] = 0; pq.push(P(d[s], s)); while (!pq.empty()) { P p = pq.top(); pq.pop(); int v = get<1>(p); if (d[v] < get<0>(p)) continue; for (const edge &e : G[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; pq.push(P(d[e.to], e.to)); } } } } }; int main() { int n,m,a,b;cin >> n >> m >> a >> b; Dijkstra g(n+1); for (int i = 0; i < m; i++) { int l,r;cin >> l >> r; g.add_edge(l-1,r,1); } for (int i = 0; i < a; i++) { g.add_edge(i+1,i,0,true); } for (int i = b; i < n; i++) { g.add_edge(i+1,i,0,true); } g.build(a-1); if (g.d[b] == g.inf) { cout << -1 << endl; } else { cout << g.d[b] << endl; } return 0; }