import sys from operator import itemgetter from collections import defaultdict, deque import heapq from heapq import heapify, heappop, _heapify_max, heappush from bisect import bisect_left, bisect_right import math import itertools import copy stdin=sys.stdin #sys.setrecursionlimit(10 ** 7) ## import pypyjit ## pypyjit.set_param('max_unroll_recursion=-1') ip=lambda: int(sp()) fp=lambda: float(sp()) lp=lambda:list(map(int,stdin.readline().split())) sp=lambda:stdin.readline().rstrip() Yp=lambda:print('Yes') Np=lambda:print('No') inf = 1 << 60 inf = float('inf') mod = 10 ** 9 + 7 mod = 998244353 eps = 1e-9 sortkey1 = itemgetter(0) sortkey2 = lambda x: (x[0], x[1]) ############################################################### N, M, d = lp() adj = [defaultdict(list) for _ in range(N)] for _ in range(M): u, v, p, q, w = lp() u -= 1; v -= 1 l = len(adj[u][v]) adj[u][v].append([w, p, q, l]) adj[v][u].append([0, 0, 0, 0]) ans = 0 while True: visit = [0 for _ in range(N)] deq = deque([(0, 0)]) before = [[-1, -1] for _ in range(N)] while deq: now, ti = deq.pop() visit[now] = 1 if now == N - 1: break for nex in adj[now].keys(): for [w, p, q, idx] in adj[now][nex]: if visit[nex] or w == 0 or (p != 0 and ti + d > p): continue deq.append((nex, q)) before[nex][0] = now before[nex][1] = idx if now != N - 1: break y = N - 1 min_flow = inf while before[y][0] != - 1: now = before[y][0] idx = before[y][1] min_flow = min(min_flow, adj[now][y][idx][0]) y = now y = N - 1 while before[y][0] != -1: now = before[y][0] idx = before[y][1] adj[now][y][idx][0] -= min_flow adj[y][now][idx][0] += min_flow y = now ans += min_flow print(ans)