import sys def MI(): return map(int, sys.stdin.readline().split()) def main(): inf=10**16 n,m,k,s,t=MI() to=[[] for _ in range(n-1)] for _ in range(m): a,b,c=MI() to[a-1].append((b,c)) dp={s:0} for i in range(n-1): if not to[i]: print(-1) exit() ndp={} for b,c in to[i]: ndp[c]=inf for f, v in dp.items(): ndp[c]=min(ndp[c],v+abs(f-b)) dp=ndp ans=inf for f,v in dp.items(): ans=min(ans,v+abs(f-t)) print(ans) main()