from collections import deque n, m, a, b = map(int, input().split()) n = b - a + 2 edges = [[] for _ in range(n)] for _ in range(m): l, r = map(int, input().split()) if l <= a and b <= r: print(1) exit() elif r < a or b < l: pass else: l = max(l, a) - a r = min(r, b) - a + 1 edges[l].append(r) edges[r].append(l) queue = deque() queue.append(0) dist = [-1] * n dist[0] = 0 while queue: pos = queue.popleft() for npos in edges[pos]: if dist[npos] != -1: continue dist[npos] = dist[pos] + 1 queue.append(npos) print(dist[-1])