#include #define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++) #define INF (1LL << 60) using namespace std; typedef long long ll; typedef pair pll; ll N, M, K, S, T, A[200000], B[200000], C[200000]; vector V, E[200000]; int main(void) { cin >> N >> M >> K >> S >> T; S--; T--; REP(i, 0, M) { cin >> A[i] >> B[i] >> C[i]; A[i]--; B[i]--; C[i]--; V.push_back(pll(A[i], B[i])); V.push_back(pll(A[i] + 1, C[i])); } V.push_back(pll(0, S)); V.push_back(pll(N - 1, T)); sort(V.begin(), V.end()); V.erase(unique(V.begin(), V.end()), V.end()); REP(i, 0, V.size() - 1) { if(V[i].first == V[i + 1].first) { ll d = abs(V[i].second - V[i + 1].second); E[i].push_back(pll(i + 1, d)); E[i + 1].push_back(pll(i, d)); } } REP(i, 0, M) { ll b = lower_bound(V.begin(), V.end(), pll(A[i], B[i])) - V.begin(); ll c = lower_bound(V.begin(), V.end(), pll(A[i] + 1, C[i])) - V.begin(); E[b].push_back(pll(c, 0)); } ll s = lower_bound(V.begin(), V.end(), pll(0, S)) - V.begin(); ll g = lower_bound(V.begin(), V.end(), pll(N - 1, T)) - V.begin(); priority_queue, greater > q; q.push(pll(0, s)); vector dp(V.size(), INF); while(q.size()) { ll d = q.top().first; ll v = q.top().second; q.pop(); if(dp[v] <= d) continue; dp[v] = d; if(v == g) break; REP(i, 0, E[v].size()) { ll nd = d + E[v][i].second; ll nv = E[v][i].first; if(dp[nv] > d) q.push(pll(nd, nv)); } } cout << (dp[g] != INF ? dp[g] : -1) << endl; }