#include <bits/stdc++.h>
using namespace std;
using ll = long long;
long long solve(int N, int M, int W, std::vector<int> T,
std::vector<int> X, std::vector<int> Y,
std::vector<int> A, std::vector<int> B, std::vector<int> C,
std::vector<int> L, std::vector<int> R){
vector<unordered_map<int, long long>> dist(N);
vector<vector<int>> in(N), out(N);
for (int i = 0; i<M; i++) {
in[Y[i]].push_back(i);
out[X[i]].push_back(i);
}
for (int i = 0; i<N; i++){
for (int e: in[i]) dist[i][B[e]] = 1e18;
}
dist[0][0] = 0;
priority_queue<tuple<long long, int, int>, vector<tuple<long long, int, int>>, greater<>> pq;
pq.push({0, 0, 0});
while(pq.size()){
auto [d, cur, t] = pq.top(); pq.pop();
if (dist[cur][t]!=d) continue;
for (int e: out[cur]){
if (A[e]>=t){
if (dist[Y[e]].find(B[e])==dist[Y[e]].end() or dist[Y[e]][B[e]]>d+C[e]){
dist[Y[e]][B[e]]=d+C[e];
pq.push({dist[Y[e]][B[e]], Y[e], B[e]});
}
}
}
}
long long res = 1e18;
for (auto e: dist[N-1]) res = min(res, e.second);
if (res==1e18) res = -1;
return res;
}
// int main() {
// cout<<solve(3, 3, 1, {20, 30, 40}, {0, 1, 0}, {1, 2, 2},
// {1, 20, 18}, {15, 30, 40}, {10, 5, 40}, {16}, {19});
// }