#include using namespace std; typedef long long ll; #define int ll typedef vector VI; typedef vector VVI; typedef vector VL; typedef vector VVL; typedef pair PII; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() #define IN(a, b, x) (a<=x&&x T &chmin(T &a, const T &b) { return a = min(a, b); } template T &chmax(T &a, const T &b) { return a = max(a, b); } template ostream &operator <<(ostream& out,const pair& a){ out<<'('< g; VI x[200010]; map d; signed main(void) { int n, m, k, s, t; cin >> n >> m >> k >> s >> t; s--, t--; REP(i, m) { int a, b, c; cin >> a >> b >> c; a--, b--, c--; // building, floor, cost g[{a, b}].PB((VI){a+1, c, 0}); x[a].PB(b); x[a+1].PB(c); } x[0].PB(s); x[n-1].PB(t); REP(i, n) { sort(ALL(x[i])); REP(j, x[i].size()-1) { // x[j] -> x[j+1] g[{i, x[i][j]}].PB((VI){i, x[i][j+1], x[i][j+1]-x[i][j]}); g[{i, x[i][j+1]}].PB((VI){i, x[i][j], x[i][j+1]-x[i][j]}); d[{i, x[i][j]}] = INF; } if(x[i].size() != 0) d[{i, x[i].back()}] = INF; } priority_queue> que; que.push({0, 0, s}); d[{0, s}] = 0; while(que.size()) { VI v = que.top(); que.pop(); int cost = v[0], build = v[1], flr = v[2]; // if(build == n-1 && flr == t) break; if(cost > d[{build, flr}]) continue; for(VI i: g[{build, flr}]) { if(d[(PII){i[0], i[1]}] > d[(PII){build, flr}] + i[2]) { d[(PII){i[0], i[1]}] = d[(PII){build, flr}] + i[2]; que.push({d[(PII){i[0], i[1]}], i[0], i[1]}); } } } if(d[{n-1, t}] >= INF) cout << -1 << endl; else cout << d[{n-1, t}] << endl; return 0; }