#include<cstdio> #include<algorithm> #include<vector> using namespace std; int N,M,K,S,T; int a[2<<17],b[2<<17],c[2<<17]; long dist[2<<17]; vector<pair<int,long> >X[2<<17]; main() { scanf("%d%d%d%d%d",&N,&M,&K,&S,&T); X[N-1].push_back(make_pair(T,-M-1)); dist[M+1]=1e18; for(int i=0;i++<M;) { dist[i]=1e18; scanf("%d%d%d",a+i,b+i,c+i); a[i]--; X[a[i]].push_back(make_pair(b[i],-i)); } X[0].push_back(make_pair(S,0)); for(int i=0;i<N;i++) { sort(X[i].begin(),X[i].end()); long now=1e18,pre=0; int n=X[i].size(); for(int j=0;j<n;j++) { now+=X[i][j].first-pre; pre=X[i][j].first; if(X[i][j].second>=0) { now=min(now,X[i][j].second); } else { int id=-X[i][j].second; dist[id]=min(dist[id],now); } } now=1e18,pre=1e9; for(int j=n;j--;) { now+=pre-X[i][j].first; pre=X[i][j].first; if(X[i][j].second>=0) { now=min(now,X[i][j].second); } else { int id=-X[i][j].second; dist[id]=min(dist[id],now); } } if(i+1<N) { for(int j=0;j<n;j++) { if(X[i][j].second<0) { int id=-X[i][j].second; X[i+1].push_back(make_pair(c[id],dist[id])); } } } } long ans=dist[M+1]; printf("%ld\n",ans<1e18?ans:-1L); }