#include using namespace std; typedef long long ll; typedef unsigned long long ull; #define REP(i, n) for(int i=0; ibool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b; //// {行き先, 容量, 逆辺} //// {to, cap, rev} //// G[v][to] <-> G[to][rev] using Edge = tuple; map> G; //// G に辺と逆辺を追加 void add_edge(P from, P to, ll cap){ G[from].push_back(Edge(to, cap, G[to].size())); G[to].push_back(Edge(from, 0, G[from].size()-1)); } //// 増加パスをdfsで探索 ll find_path(P v, P t, ll f, auto& used){ if(v == t) return f; used.insert(v); for(auto& [to, cap, rev] : G[v]){ if(used.find(to) == used.end() && cap > 0){ ll d = find_path(to, t, min(f, cap), used); if(d > 0){ cap -= d; get<1>(G[to][rev]) += d; return d; } } } return 0; } //// sからtへの最大流を求める ll max_flow(P s, P t){ ll flow = 0; while(1){ set

used; ll f = find_path(s, t, INF, used); if(f == 0) break; flow += f; } return flow; } int main(){ ll N, M, D; cin >> N >> M >> D; using T = tuple; vector X; REP(i,M){ ll u, v, p, q, w; cin >> u >> v >> p >> q >> w; u--, v--; if(q+D > 1000000000LL) continue; X.push_back(T(u, v, p, q, w)); } vector> vertex(N); for(auto& [u, v, p, q, w] : X){ vertex[u].insert(p); vertex[v].insert(q+D); add_edge(P(u, p), P(v, q+D), w); //add_edge(P(u, p), P(v, q), w); } REP(v, N){ auto vec = vector(vertex[v].begin(), vertex[v].end()); int size = vec.size(); REP(i,size){ REPi(j,i+1,size){ add_edge(P(v, vec[i]), P(v, vec[j]), INF); } } } if(vertex[0].size() == 0 || vertex[N-1].size() == 0){ cout << 0 << endl; return 0; } ll ans = max_flow(P(0, *vertex[0].begin()), P(N-1, *prev(vertex[N-1].end(), 1))); cout << ans << endl; return 0; }