#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 FordFulkerson { ll n; vector> g; vector used; FordFulkerson(ll n) : n(n), g(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); } ll dfs(ll v, ll t, ll f) { if(v == t) return f; // 終点に到達した時のminのflow used[v] = true; for(ll i = 0; i < (ll)g[v].size(); ++i) { Edge& e = g[v][i]; if(!used[e.to] && e.cap > 0) { ll d = dfs(e.to, t, min(f, e.cap)); // 先に進むごとにe.capとのminに減らす if(d > 0) { e.cap -= d; g[e.to][e.rev].cap += d; return d; } } } return 0; // 終点に到達しなければflowの増量は0 } ll max_flow(ll s, ll t) { ll flow = 0; while(true) { used.assign(n, false); ll f = dfs(s, t, INFL); // 終点に到達した時のminのflow if(f == 0) return flow; 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]; FordFulkerson 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(); }