#https://yukicoder.me/problems/no/654 import copy class FordFulkerson: def __init__(self,v): self.v = V self.edges = {} self.used = {} def addEdge(self,fr,to,cap): forward = [to,cap,None] backward = forward[2] = [fr,0,forward] # self.edges.get(fr,[]).append(forward) # self.edges.get(to,[]).append(backward) if fr in self.edges: self.edges[fr].append(forward) else: self.edges[fr] = [forward] if to in self.edges: self.edges[to].append(backward) else: self.edges[to] = [backward] self.used[fr] = False self.used[to] = False def dfs(self,v,t,f,used): if v == t: return f used[v] = True for e in self.edges[v]: w,cap,rev = e if used[w] is False and cap: d = self.dfs(w,t,min(f,cap),used) if d: e[1] -= d rev[1] += d return d return 0 def flow(self,s,t): flow = 0 f = INF = float('inf') v = self.v while f: used = self.used.copy() #浅いコピー f = self.dfs(s,t,INF,used) flow += f return flow N,M,d = map(int,input().split()) V = N #頂点の数 E = M #辺の数 #(u,pi)→(v,qi)にcapacityがwiの辺を設定 #(u,pi)→(u,pi+1)には容量無限大の辺を設定(空港で待つことを意味する) #上の方法だと間に合わないから #辺を貼るのは街iに飛行機が到着する時刻だけでいい。 INF = float('inf') ff = FordFulkerson(N+1) visited = [[] for _ in range(N+1)] #各街に飛行機が到着する時間 visited[1].append(0) visited[N].append(1e9) #dを考慮する。Aに出発しBに到着する飛行機はAに出発し、B+dに到着するとみなすことができる。 for _ in range(M): u,v,p,q,w = map(int,input().split()) #(u,p)→(v,q+d)へcapacityがwの辺 visited[u].append(p) visited[v].append(q+d) ff.addEdge((u,p),(v,q+d),w) # print(ff.edges) for i in range(N+1): visited[i].sort() for j in range(len(visited[i])-1): ff.addEdge((i,visited[i][j]),(i,visited[i][j+1]),INF) print(ff.flow((1,0),(N,1e9)))