#include using namespace std; using state = tuple; // (cost, tou, kai) const int MAX = 2e5 + 10; int N, M, S, T; long K; vector> G[MAX]; unordered_map mp[MAX]; //[tou][kai] = minCost int main() { scanf("%d%d%lld%d%d", &N, &M, &K, &S, &T); S--;T--;K--; for (int i = 0; i < M; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); a--;b--;c--; G[a].emplace_back(b, c); } priority_queue, greater> pq; pq.emplace(0, 0, S); const long long inf = 1e18; long long ans = inf; while (!pq.empty()) { long long cost; int tou, kai; tie(cost, tou, kai) = pq.top(); pq.pop(); if (tou == N - 1) { ans = min(ans, cost + llabs(kai - T)); } if (mp[tou].count(kai)) { continue; } mp[tou][kai] = cost; for (auto&& e : G[tou]) { if (mp[tou + 1].count(e.second) && mp[tou + 1][e.second] <= cost + llabs(e.first - kai)) { continue; } pq.emplace(cost + llabs(e.first - kai), tou + 1, e.second); } } cout << (ans == inf ? -1 : ans) << endl; return 0; }