#include using namespace std; template class dinic{ struct edge{ int to; uint64_t cap; int rev; }; vector edges[MAX_V]; int level[MAX_V]; int next[MAX_V]; void set_level(int s){ memset(level, -1, sizeof(level)); queue Q; level[s] = 0; Q.push(s); while(!Q.empty()){ int v = Q.front(); Q.pop(); for(edge e:edges[v]){ if(e.cap > 0 && level[e.to] < 0){ level[e.to] = level[v] + 1; Q.push(e.to); } } } } uint64_t blockingflow(int v, int t, uint64_t f){ if(v == t) return f; for(int &i = next[v];i 0 && level[v] < level[e.to]){ uint64_t d = blockingflow(e.to, t, min(f, e.cap)); if(d > 0){ e.cap -= d; edges[e.to][e.rev].cap += d; return d; } } } return 0; } public: void add_edge(int from, int to, uint64_t cap){ edges[from].push_back({to, cap, (int)edges[to].size()}); edges[to].push_back({from, 0, (int)edges[from].size()-1}); } uint64_t max_flow(int s, int t){ uint64_t flow = 0; while(true){ set_level(s); if(level[t] < 0) return flow; memset(next, 0, sizeof(next)); int f; while((f = blockingflow(s, t, -1)) > 0) flow += f; } } }; struct flight{ int from, to, dept, arrv, cap; }; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout.precision(12); cout.setf(ios_base::fixed, ios_base::floatfield); dinic<2002> G; flight dat[1001]; int n, m, d; cin >> n >> m >> d; for(int i=1;i<=m;i++){ int u, v, p, q, w; cin >> u >> v >> p >> q >> w; dat[i] = {u-1, v-1, p, q, w}; G.add_edge(2*i, 2*i+1, w); } for(int i=1;i<=m;i++){ if(dat[i].from == 0) G.add_edge(0, 2*i, -1); if(dat[i].to == n-1) G.add_edge(2*i+1, 1, -1); for(int j=1;j<=m;j++){ if(i==j || dat[i].to!=dat[j].from) continue; if(dat[i].arrv+d <= dat[j].dept) G.add_edge(2*i+1, 2*j, INT32_MAX); } } cout << G.max_flow(0, 1) << endl; return 0; }