import sys from collections import deque readline = sys.stdin.readline class Edge(): def __init__(self, to, cap, rev): self.to = to self.cap = cap self.rev = rev class Dinic(): def __init__(self, N, inf = float('inf')): self.N = N self.G = [[] for _ in range(N)] self.level = [-1]*N self.it = [0]*N self.inf = inf def add_edge(self, fr, to, cap): self.G[fr].append(Edge(to, cap, len(self.G[to]))) self.G[to].append(Edge(fr, 0, len(self.G[fr]) - 1)) def bfs(self, s): self.level = [-1]*self.N q = deque([s]) self.level[s] = 0 while q: v = q.popleft() for i in range(len(self.G[v])): e = self.G[v][i] if e.cap > 0 and self.level[e.to] < 0: self.level[e.to] = self.level[v] + 1 q.append(e.to) def dfs(self, v, t, f): if v == t: return f for i in range(self.it[v], len(self.G[v])): e = self.G[v][i] if e.cap > 0 and self.level[v] < self.level[e.to]: d = self.dfs(e.to, t, min(f, e.cap)) if d > 0: e.cap -= d self.G[e.to][e.rev].cap += d return d return 0 def max_flow(self, s, t): flow = 0 while True: self.bfs(s) if self.level[t] < 0: return flow self.it = [0]*self.N f = self.dfs(s, t, self.inf) while f > 0: flow += f f = self.dfs(s, t, self.inf) def main(): INF = float('inf') N, M, d = map(int, readline().split()) dinic = Dinic(2*M + 2) vertex = [None]*(2*M + 2) for i in range(1, M + 1): u, v, p, q, w = map(int, readline().split()) vertex[i] = (u, p) vertex[i + M] = (v, q) dinic.add_edge(i, i + M, w) for i in range(M + 1, 2*M + 1): v, q = vertex[i] for j in range(1, M + 1): u, p = vertex[j] if v == u and q + d <= p: dinic.add_edge(i, j, INF) for i in range(1, M + 1): u, p = vertex[i] if u == 1: dinic.add_edge(0, i, INF) v, q = vertex[i + M] if v == N: dinic.add_edge(i + M, 2*M + 1, INF) f = dinic.max_flow(0, 2*M + 1) print(f) if __name__ == '__main__': main()