#include using namespace std; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define ll long long #define MOD 1000000007 #define MOD2 998244353 #define INF 1e9 struct edge{ ll to, cap, rev, p, q; }; vector> G; vector used; ll n, m; void add_edge(ll u, ll v, ll c, ll p, ll q){ G[u].push_back((edge){v, c, (ll)G[v].size(), p, q}); G[v].push_back((edge){u, 0, (ll)G[u].size()-1, (ll)INF, p}); } ll dfs(ll v, ll t, ll f, int time){ if(time > 1e9)return 0; if(v == t)return f; used[v] = 1; rep(i, G[v].size()){ edge &e = G[v][i]; if(time > G[v][i].p) continue; if(e.cap > 0 && !used[e.to]){ ll d = dfs(e.to, t, min(f, e.cap), G[v][i].q); if(d > 0){ G[v][i].cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } ll max_flow(ll s, ll t){ ll flow = 0; for(;;){ for(ll i=0; i> n >> m >> d; G.resize(n); used.resize(n); rep(i, m){ ll u, v, p, q, w; cin >> u >> v >> p >> q >> w; u--, v--; if(u != 0)add_edge(u, v, w, p-d, q); else add_edge(u, v, w, p, q); } cout << max_flow(0, n-1) << endl; return 0; }