import heapq INF = 1 << 60 n, m, k, s, t = map(int, input().split()) s -= 1 t -= 1 idx = {} idx.setdefault((0, s), len(idx)) idx.setdefault((n - 1, t), len(idx)) adj = [[0, k - 1] for _ in range(n)] adj[0].append(s) adj[n - 1].append(t) arcs = [] for _ in range(m): a, b, c = map(int, input().split()) a -= 1 b -= 1 c -= 1 idx.setdefault((a, b), len(idx)) idx.setdefault((a + 1, c), len(idx)) arcs.append((idx[(a, b)], idx[(a + 1, c)], 0)) adj[a].append(b) adj[a + 1].append(c) for b in range(n): floors = sorted(list(set(adj[b]))) for i in range(len(floors) - 1): idx.setdefault((b, floors[i]), len(idx)) idx.setdefault((b, floors[i + 1]), len(idx)) u = idx[(b, floors[i])] v = idx[(b, floors[i + 1])] cost = floors[i + 1] - floors[i] arcs.append((u, v, cost)) arcs.append((v, u, cost)) g = [[] for _ in range(len(idx))] for u, v, c in arcs: g[u].append((v, c)) dist = [INF for _ in range(len(idx))] src = idx[(0, s)] dst = idx[(n - 1, t)] dist[src] = 0 hp = [(dist[src], src)] while len(hp) > 0: cd, cur = heapq.heappop(hp) if cd > dist[cur]: continue for nxt, cost in g[cur]: if dist[cur] + cost < dist[nxt]: dist[nxt] = dist[cur] + cost heapq.heappush(hp, (dist[nxt], nxt)) if dist[dst] < INF: print(dist[dst]) else: print("-1")