#include using namespace std; using VI = vector; using VVI = vector; using PII = pair; using LL = long long; using VL = vector; using VVL = vector; using PLL = pair; using VS = vector; #define ALL(a) begin((a)),end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define EB emplace_back #define MP make_pair #define SZ(a) int((a).size()) #define SORT(c) sort(ALL((c))) #define RSORT(c) sort(RALL((c))) #define UNIQ(c) (c).erase(unique(ALL((c))), end((c))) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define FF first #define SS second template istream& operator>>(istream& is, pair& p){ return is >> p.FF >> p.SS; } template ostream& operator<<(ostream& os, const pair& p){ return os << p.FF << " " << p.SS; } template void maxi(T& x, T y){ if(x < y) x = y; } template void mini(T& x, T y){ if(x > y) x = y; } const double EPS = 1e-10; const double PI = acos(-1.0); const LL MOD = 1e9+7; void order(map& m){ PLL prv(-1, -1); for(auto& p: m){ if(prv.FF >= 0){ mini(p.SS, abs(p.FF - prv.FF) + prv.SS); } prv = p; } prv = MP(-1,-1); for(auto rit=m.rbegin();rit!=m.rend();++rit){ if(prv.FF >= 0){ mini(rit->SS, abs(rit->FF - prv.FF) + prv.SS); } prv = *rit; } } int main(){ cin.tie(0); ios_base::sync_with_stdio(false); int N, M; LL K, S, T; cin >> N >> M >> K >> S >> T; vector> as(N-1); REP(i,M){ int a, b, c; cin >> a >> b >> c; as[a-1].EB(b,c); } REP(i,N-1) SORT(as[i]); map dp[2]; int crt = 0, nxt = 1; dp[crt][S] = 0; REP(a,N-1){ if(dp[crt].empty()){ break; } dp[nxt].clear(); order(dp[crt]); auto it = begin(dp[crt]); auto nit = it; ++nit; for(auto& p: as[a]){ while(nit != end(dp[crt]) && nit->FF < p.FF){ LL cost = abs(it->FF - p.FF) + it->SS; if(!dp[nxt].count(p.SS)) dp[nxt][p.SS] = cost; else mini(dp[nxt][p.SS], cost); ++it; ++nit; } LL cost = abs(it->FF - p.FF) + it->SS; if(!dp[nxt].count(p.SS)) dp[nxt][p.SS] = cost; else mini(dp[nxt][p.SS], cost); if(nit != end(dp[crt])){ LL cost = abs(nit->FF - p.FF) + nit->SS; if(!dp[nxt].count(p.SS)) dp[nxt][p.SS] = cost; else mini(dp[nxt][p.SS], cost); } } swap(crt, nxt); } if(dp[crt].empty()){ cout << -1 << endl; } else{ LL ans = 1e18; for(auto& p: dp[crt]) mini(ans, abs(p.FF - T) + p.SS); cout << ans << endl; } return 0; }