#include #include using namespace std; int main() { cin.tie(0)->sync_with_stdio(0); int N, M, d; cin >> N >> M >> d; vector u(M), v(M), p(M), q(M), w(M); for (int i = 0; i < M; i++) { cin >> u[i] >> v[i] >> p[i] >> q[i] >> w[i]; u[i]--; v[i]--; } map, int> index; // {city_id, time} -> index of G vector> times(N); int id = 0; index[{ 0, 0 }] = id++; times[0].push_back(0); times[N - 1].push_back(1e9); for (int i = 0; i < M; i++) { index[{ u[i], p[i] }] = id++; index[{ v[i], q[i] + d }] = id++; times[u[i]].push_back(p[i]); times[v[i]].push_back(q[i] + d); } index[{ N - 1, 1e9 }] = id++; atcoder::mf_graph G(id); for (int i = 0; i < M; i++) { G.add_edge(index[{ u[i], p[i] }], index[{ v[i], q[i] + d }], w[i]); } for (int i = 0; i < N; i++) { sort(times[i].begin(), times[i].end()); for (int j = 0; j < times[i].size() - 1; j++) { G.add_edge(index[{ i, times[i][j] }], index[{ i, times[i][j + 1] }], 7 + 1e9); } } int flow = G.flow(index[{ 0, 0 }], index[{ N - 1, 1e9 }]); cout << flow << endl; }