from collections import defaultdict import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) inf = 10**18 N, M, K, S, T = map(int, input().split()) if N == 1: print(abs(S - T)) exit() stair = [[] for _ in range(N+1)] stair[1].append((S, ~S)) ok = [0] * N for _ in range(M): a, b, c = map(int, input().split()) ok[a] = 1 stair[a].append((b, 1)) stair[a+1].append((c, ~b)) dp = defaultdict(lambda: inf) dp[S] = 0 for i in range(1, N): if not ok[i]: print(-1) exit() ndp = defaultdict(lambda: inf) stair[i].sort(key=lambda x: (x[0], x[1])) vmin = inf for s, f in stair[i]: if f >= 0: ndp[s] = min(ndp[s], s + vmin) else: f = ~f vmin = min(vmin, dp[f] - s) vmin = inf stair[i].sort(key=lambda x: (x[0], -x[1])) for s, f in stair[i][::-1]: if f >= 0: ndp[s] = min(ndp[s], s + vmin) else: f = ~f vmin = min(vmin, dp[f] + s) dp = ndp ans = inf for s, f in stair[N]: ans = min(ans, abs(T - s) + dp[~f]) print(ans)