#include using namespace std; using ll = long long; using P = pair; using Vec = vector; #define REP(i, m, n) for(ll i = (m); i < (n); ++i) #define rep(i, n) REP(i, 0, n) template bool chmax(T &a, const T b){if(a < b){a = b; return true;} return false;} template bool chmin(T &a, const T b){if(a > b){a = b; return true;} return false;} constexpr ll LINF = 1e18L+1; constexpr ll MOD = 1e9+7; using Graph = map>; set

used; Graph graph; ll dfs(P v, ll t, ll f){ cerr << v.first << " " << v.second << endl; if (v.first == t) return f; auto p = graph.lower_bound(v); while(p->first.first == v.first){ for(auto &q : p->second){ if (used.find(q.first) == used.end() && q.second) { used.insert(q.first); ll d = dfs(q.first, t, min(f, q.second)); if (d) { q.second -= d; graph[q.first][v] += d; return d; } } } ++p; } return 0; } // 最大流を求める ll ford_fulkerson(ll s, ll t){ ll res = 0; while(true){ used.clear(); ll f = dfs({s, 0}, t, LINF); cerr << f << endl; if (!f) break; res += f; } return res; } int main(void) { ll V, E, d; cin >> V >> E >> d; rep(i, E){ ll u, v, p, q, w; cin >> u >> v >> p >> q >> w; --u, --v, q += d; graph[{u, p}][{v, q}] = w; } cout << ford_fulkerson(0, V - 1) << endl; return 0; }