#include using namespace std; typedef long long ll; typedef long double ld; typedef pair Pii; typedef pair Pil; typedef pair Pll; typedef pair Pli; #define fi first #define se second const ll MOD = 1e9 + 7; const ll MOD2 = 998244353; const ll MOD3 = 1812447359; const ll INF = 1ll << 62; const double PI = 2 * asin(1); void yes() {printf("yes\n");} void no() {printf("no\n");} void Yes() {printf("Yes\n");} void No() {printf("No\n");} void YES() {printf("YES\n");} void NO() {printf("NO\n");} int N, M, D; struct edge {int to, cap, rev, time_p, time_q;}; vector Graph[1005]; bool used[1005]; int Add_Edge(int from, int to, int cap, int time_p, int time_q){ int rev = Graph[to].size(); Graph[from].push_back((edge){to, cap, rev, time_p, time_q}); rev = Graph[from].size() - 1; Graph[to].push_back((edge){from, 0, rev, time_p, time_q}); return 0; } int DFS(int S, int T, int Flow, int Time){ if (S == T) return Flow; used[S] = true; for (int i = 0; i < Graph[S].size(); i++){ edge &E = Graph[S][i]; if (!used[E.to] && E.cap > 0 && E.time_p >= Time){ int F = DFS(E.to, T, min(Flow, E.cap), E.time_q + D); if (F > 0){ E.cap -= F; Graph[E.to][E.rev].cap += F; return F; } } } return 0; } int main(){ cin >> N >> M >> D; for (int i = 0; i < M; i++){ int U, V, P, Q, W; cin >> U >> V >> P >> Q >> W; Add_Edge(U, V, W, P, Q); } int ans = 0; while (true){ fill(used, used + N + 1, false); int Flow = DFS(1, N, MOD, 0); if (Flow == 0) break; ans += Flow; } cout << ans << endl; return 0; }