#include using namespace std; #include using namespace atcoder; int main() { int n, m, d; cin >> n >> m >> d; vector> planes(m); vector> us; us.emplace_back(0, 0); us.emplace_back(n - 1, INT_MAX); for (auto& [u, v, p, q, w] : planes) { cin >> u >> v >> p >> q >> w; u--; v--; q += d; us.emplace_back(u, p); us.emplace_back(v, q); } sort(us.begin(), us.end()); us.erase(unique(us.begin(), us.end()), us.end()); mf_graph g(us.size()); for (int i = 0; i < (int)us.size() - 1; ++i) { if (us[i].first == us[i + 1].first) { g.add_edge(i, i + 1, LLONG_MAX); } } for (auto [u, v, p, q, w] : planes) { int i = (int)distance(us.begin(), lower_bound(us.begin(), us.end(), make_pair(u, p))); int j = (int)distance(us.begin(), lower_bound(us.begin(), us.end(), make_pair(v, q))); g.add_edge(i, j, w); } cout << g.flow(0, us.size() - 1) << '\n'; }