import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from collections import deque INF = 10**18 n, m, a, b = map(int, input().split()) edge = [[] for i in range(n+2)] for i in range(m): l, r = map(int, input().split()) r += 1 l = max(l, a) r = min(r, b+1) edge[l].append(r) edge[r].append(l) q = deque([]) q.append(a) dist = [INF]*(n+2) dist[a] = 0 while q: v = q.popleft() for u in edge[v]: if dist[u] != INF: continue dist[u] = dist[v]+1 q.append(u) ans = dist[b+1] if ans < INF: print(ans) else: print(-1)