mod = 1000000007 eps = 10**-9 def main(): import sys from collections import deque input = sys.stdin.buffer.readline N, M, A, B = map(int, input().split()) N += 1 adj = [set() for _ in range(N+1)] for _ in range(M): l, r = map(int, input().split()) r += 1 l = max(l, A) r = min(r, B+1) adj[l].add(r) adj[r].add(l) que = deque() que.append(A) seen = [-1] * (N+1) seen[A] = 0 par = [0] * (N+1) child = [[] for _ in range(N+1)] seq = [] while que: v = que.popleft() seq.append(v) for u in adj[v]: if seen[u] == -1: seen[u] = seen[v] + 1 par[u] = v child[v].append(u) que.append(u) seq.reverse() print(seen[B+1]) if __name__ == '__main__': main()