#include #include #include #include #include using namespace std; int main() { int n, m, k, s, t; cin >> n >> m >> k >> s >> t; vector a(m), b(m), c(m); vector> g(n), gg(n); g[0].push_back(s); g[n - 1].push_back(t); for (int i = 0; i < m; i++) { scanf("%d %d %d", &a[i], &b[i], &c[i]); g[a[i] - 1].push_back(b[i]); g[a[i] ].push_back(c[i]); gg[a[i] - 1].push_back(i); } vector> dp(n); for (int i = 0; i < n; i++) { sort(g[i].begin(), g[i].end()); g[i].erase(unique(g[i].begin(), g[i].end()), g[i].end()); dp[i].resize(g[i].size(), 1e18); } auto index = [&](vector &a, int v) { return lower_bound(a.begin(), a.end(), v) - a.begin(); }; int S = index(g[0], s); int T = index(g[n - 1], t); dp[0][S] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j + 1 < g[i].size(); j++) { dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + g[i][j + 1] - g[i][j]); } for (int j = (int)g[i].size() - 2; j >= 0; j--) { dp[i][j] = min(dp[i][j], dp[i][j + 1] + g[i][j + 1] - g[i][j]); } for (int j : gg[i]) { int u = index(g[i], b[j]); int v = index(g[i + 1], c[j]); dp[i + 1][v] = min(dp[i + 1][v], dp[i][u]); } } if (dp[n - 1][T] >= 1e17) { cout << -1 << endl; } else { cout << dp[n - 1][T] << endl; } }