import sys readline = sys.stdin.readline MAX = int(1e18) N, M, K, S, T = map(int, readline().split()) R = [[] for _ in range(N)] R[0].append([0, S, 0]) for _ in range(M): a, b, c = map(int, readline().split()) R[a].append([b, c, MAX]) for i in range(1, N): R[i].sort() k_max = len(R[i - 1]) - 1 k = 0 min_val = MAX for j in range(len(R[i])): while k <= k_max and R[i - 1][k][1] <= R[i][j][0]: min_val = min(min_val, R[i - 1][k][2] - R[i - 1][k][1]) k += 1 R[i][j][2] = min(R[i][j][2], min_val + R[i][j][0]) k = k_max min_val = MAX for j in range(len(R[i]) - 1, -1, -1): while k >= 0 and R[i - 1][k][1] >= R[i][j][0]: min_val = min(min_val, R[i - 1][k][2] + R[i - 1][k][1]) k -= 1 R[i][j][2] = min(R[i][j][2], min_val - R[i][j][0]) R[i].sort(key=lambda x:x[1]) ans = MAX for r in R[N - 1]: ans = min(ans, abs(r[1] - T) + r[2]) print(ans if ans < MAX else -1)