from collections import deque INF = float('inf') class EDOMONDS_KARP(): def __init__(self, N, s, t): self.N = N; self.s = s; self.t = t self.cap = [[0]*N for _ in range(N)] self.link = [[] for _ in range(N)] def add_edge(self, u, v, c): self.cap[u][v] = c self.link[u].append(v) self.link[v].append(u) def max_flow(self): N = self.N; s = self.s; t = self.t f = 0 flow = [[0]*N for _ in range(N)] while True: m, prev = self.bfs(flow) if m == 0: break f += m v = t while v != s: u = prev[v] flow[u][v] += m flow[v][u] -=m v = u return (f, flow) def bfs(self, flow): N = self.N; s = self.s; t = self.t cap = self.cap link = self.link prev = [-1]*N; prev[s] = -2 m = [0]*N; m[s] = INF q = deque([s]) while q: u = q.popleft() for v in link[u]: if cap[u][v] - flow[u][v] > 0 and prev[v] == -1: prev[v] = u m[v] = min(m[u], cap[u][v] - flow[u][v]) if v != t: q.append(v) else: return (m[t], prev) return (0, prev) N, M, d = map(int, input().split()) S, T = 0, 2*M + 1 EK = EDOMONDS_KARP(2*M + 2, S, T) start = [[] for _ in range(N + 1)] stop = [[] for _ in range(N + 1)] for i in range(1, M + 1): u, v, p, q, w = map(int, input().split()) EK.add_edge(i, i + M, w) start[u].append((p, i)) stop[v].append((q, i + M)) for _, i in start[1]: EK.add_edge(S, i, INF) for _, i in stop[N]: EK.add_edge(i, T, INF) for v in range(1, N + 1): for q, i in stop[v]: for p, j in start[v]: if q + d <= p: EK.add_edge(i, j, INF) f, _ = EK.max_flow() print(f)