#include using namespace std; typedef long long ll; typedef pair PII; const int MM = 1e9 + 7; const double eps = 1e-8; const int MAXN = 2e6 + 10; int n, m; void prework(){ } void read(){ } ll K; int S, T; vector E[MAXN]; vector F[MAXN]; vector D[MAXN]; const ll INF = 1ll << 60; unordered_map tag1, tag2; void solve(int casi){ // cout << "Case #" << casi << ": "; cin >> n >> m >> K >> S >> T; E[1].push_back(S); E[n].push_back(T); for (int i = 1; i <= n; i++){ int x, y, z; cin >> x >> y >> z; E[x].push_back(y); E[x+1].push_back(z); F[x].push_back(PII(y, z)); } sort(E[1].begin(), E[1].end()); for (auto &x : E[1]){ D[1].push_back(abs(x - S)); } for (int i = 1; i < n; i++){ tag1.clear(); tag2.clear(); sort(E[i+1].begin(), E[i+1].end()); sort(F[i].begin(), F[i].end()); D[i+1].resize(E[i+1].size()); int n2 = E[i+1].size(), n1 = E[i].size(); for (int j = 0; j < E[i].size(); j++) tag1[E[i][j]] = j; for (int j = 0; j < E[i + 1].size(); j++) tag2[E[i + 1][j]] = j; for (int j = 0; j < n2; j++) D[i+1][j] = INF; for (auto &x : F[i]){ D[i+1][tag2[x.second]] = min(D[i+1][tag2[x.second]], D[i][tag1[x.first]]/* + abs(x.first - x.second)*/); } for (int j = 1; j < n2; j++){ D[i+1][j] = min(D[i+1][j], D[i+1][j-1] + abs(E[i+1][j] - E[i+1][j-1])); } for (int j = n2 - 2; j >= 0; j--){ D[i+1][j] = min(D[i+1][j], D[i+1][j+1] + abs(E[i+1][j] - E[i+1][j+1])); } } if (D[n][tag2[T]] >= INF) D[n][tag2[T]] = -1; cout << D[n][tag2[T]] << endl; } void printans(){ } int main(){ std::ios::sync_with_stdio(false); prework(); int T = 1; // cin>>T; for(int i = 1; i <= T; i++){ read(); solve(i); printans(); } return 0; }