from collections import deque n,m,a,b=map(int,input().split()) g=[[] for _ in range(n+2)] q=deque([]) for _ in range(m): l,r=map(int,input().split()) g[l].append(r+1) g[r+1].append(l) if l<=a: q.append((0,l)) d=[-1]*(n+2) while q: dd,i=q.popleft() if 0<=d[i]<=dd: continue d[i]=dd for j in g[i]: if d[j]==-1: q.append((dd+1,j)) ans=-1 for i in range(b+1,n+2): if d[i]>=-1: if ans==-1: ans=d[i] ans=min(ans,d[i]) print(ans)