#include using namespace std; using ll = long long; using pll = pair; #define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i) #define rep(i, n) drep(i, 0, n - 1) #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second const ll MOD = 1000000007; const ll MOD2 = 998244353; const ll INF = 1LL << 60; const ll MAX_N = 2e5; struct Edge{ ll to; ll cap; ll rev; }; struct Ford_Fulkerson{ vector> G; vector used; ll N; Ford_Fulkerson(vector> G_){ N = (ll)G_.size(); G.resize(N); used.resize(N, false); rep(from, N){ for(pll p : G_[from]){ ll to = p.fi; ll cap = p.se; G[from].pb((Edge){to, cap, (ll)G[to].size()}); G[to].pb((Edge){from, 0, (ll)(G[from].size()-1)}); } } } ll dfs(ll v, ll t, ll f, vector> &H){ if(v == t) return f; used[v] = true; for(ll i=0; i<(ll)H[v].size(); i++){ Edge &e = H[v][i]; if(!used[e.to] && e.cap > 0){ ll d = dfs(e.to, t, min(f, e.cap), H); if(d > 0){ e.cap -= d; H[e.to][e.rev].cap += d; return d; } } } return 0; } ll max_flow(ll s, ll t){ ll flow = 0; vector> H(N); rep(from, N) for(Edge e : G[from]) H[from].pb(e); for(;;){ rep(i, N) used[i] = false; ll f = dfs(s, t, INF, H); if(f == 0) return flow; flow += f; } } }; int main(){ ll n, m, d; cin >> n >> m >> d; vector>> G_in(n); // (自身のID, 到着時刻) vector>> G_out(n); // (自身のID, 相手のID, 出発時刻, 容量) ll cnt = 1; rep(i, m){ ll u, v, p, q, w; cin >> u >> v >> p >> q >> w; u--; v--; if(u == n-1 || v == 0) continue; ll u_id = cnt++; ll v_id = cnt++; G_in[v].pb({v_id, q}); G_out[u].pb({u_id, v_id, p, w}); } cnt++; vector> G(cnt); for(tuple tmp : G_out[0]){ ll v = get<0>(tmp); G[0].pb({v, INF}); } for(ll i=1; i tmp1 : G_in[i]){ ll u = get<0>(tmp1); ll p = get<1>(tmp1); for(tuple tmp2 : G_out[i]){ ll v = get<0>(tmp2); ll q = get<2>(tmp2); if(p + d <= q){ G[u].pb({v, INF}); } } } } for(tuple tmp : G_in[n-1]){ ll u = get<0>(tmp); G[u].pb({cnt-1, INF}); } for(ll i=0; i tmp : G_out[i]){ ll u = get<0>(tmp); ll v = get<1>(tmp); ll c = get<3>(tmp); G[u].pb({v, c}); } } /* for(ll i=0; i