#include //#include using namespace std; //using namespace atcoder; using ll = long long; using vll = vector; using vvll = vector; using pll = pair; using vpll = vector; using ld = long double; using vld = vector; using vb = vector; #define rep(i, n) for (ll i = 0; i < (n); i++) #ifdef LOCAL #define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl #else #define dbg(x) true #endif template bool chmin(T& a, T b) { if(a > b) { a = b; return true; } else return false; } template bool chmax(T& a, T b) { if(a < b) { a = b; return true; } else return false; } template ostream& operator<<(ostream& s, const vector& a) { for(auto i : a) s << i << ' '; return s; } constexpr int INF = 1 << 30; constexpr ll INFL = 1LL << 60; constexpr ld EPS = 1e-12; ld PI = acos(-1.0); struct Edge { ll to, cap, rev; Edge(ll to, ll cap, ll rev) : to(to), cap(cap), rev(rev) {}; }; struct Dinic { ll n; vector> g; vector level, iter; Dinic(ll n) : n(n), g(n), iter(n) {}; void add_edge(ll from, ll to, ll cap) { g[from].emplace_back(to, cap, (ll)g[to].size()); g[to].emplace_back(from, 0, (ll)g[from].size()-1); } // 容量が残っているパスにおける始点からの距離を計算 void bfs(ll s) { level.assign(n, -1); queue que; level[s] = 0; que.push(s); while(!que.empty()) { ll v = que.front(); que.pop(); for(ll i = 0; i < (ll)g[v].size(); ++i) { Edge& e = g[v][i]; if(e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } // levelが増えるパスにおけるflowを計算 ll dfs(ll v, ll t, ll f) { if(v == t) return f; // iter[v]を増やすことで各Edgeの探査は1回のみとする for(ll& i = iter[v]; i < (ll)g[v].size(); ++i) { Edge& e = g[v][i]; if(e.cap > 0 && level[v] < level[e.to]) { ll d = dfs(e.to, t, min(f, e.cap)); if(d > 0) { e.cap -= d; g[e.to][e.rev].cap += d; return d; } } } return 0; } ll max_flow(ll s, ll t) { ll flow = 0; while(true) { bfs(s); if(level[t] < 0) return flow; iter.assign(n, 0); ll f; while((f = dfs(s, t, INFL)) > 0) { flow += f; } } } }; void solve() { ll n, m, d; cin >> n >> m >> d; vll u(m), v(m), p(m), q(m), w(m); rep(i, m) cin >> u[i] >> v[i] >> p[i] >> q[i] >> w[i]; Dinic D(2*m+2); rep(i, m) { D.add_edge(i, i+m, w[i]); if(u[i] == 1) D.add_edge(2*m, i, w[i]); if(v[i] == n) D.add_edge(i+m, 2*m+1, w[i]); } rep(fr, m) rep(to, m) { if(v[fr] == u[to] && q[fr] + d <= p[to]) { D.add_edge(fr+m, to, max(w[fr], w[to])); } } cout << D.max_flow(2*m, 2*m+1) << endl; return; } int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); solve(); }