from collections import deque import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) inf = 10**9 N, M, A, B = map(int, input().split()) source = 0 sink = N + 2 G = [[] for _ in range(N + 3)] for _ in range(M): L, R = map(int, input().split()) R += 1 G[L].append((R, 1)) G[R].append((L, 1)) for i in range(1, A + 1): G[source].append((i, 0)) for i in range(B + 1, N + 2): G[i].append((sink, 0)) d = [inf] * (N + 3) d[source] = 0 q = deque([source]) while q: s = q.popleft() ds = d[s] for t, dt in G[s]: if d[t] >= inf: if dt == 0: d[t] = ds q.appendleft(t) else: d[t] = ds + dt q.append(t) ans = d[sink] if ans >= inf: ans = -1 print(ans)