import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class Main { public static final long INF = Long.MAX_VALUE / 2 - 1; public static long dinic(Vertex s, Vertex t) { long flow = 0; for (int p = 1;; p++) { Queue que = new LinkedList(); s.level = 0; s.p = p; que.offer(s); while (!que.isEmpty()) { Vertex v = que.poll(); v.iter = v.es.size() - 1; for (Edge e : v.es) if (e.to.p < p && e.cap > 0) { e.to.level = v.level + 1; e.to.p = p; que.offer(e.to); } } if (t.p < p) { return flow; } for (long f; (f = dfs(s, t, INF)) > 0;) { flow += f; } } } public static long dfs(Vertex v, Vertex t, long f) { if (v == t) { return f; } for (; v.iter >= 0; v.iter--) { Edge e = v.es.get(v.iter); if (v.level < e.to.level && e.cap > 0) { long d = dfs(e.to, t, Math.min(f, e.cap)); if (d > 0) { e.cap -= d; e.rev.cap += d; return d; } } } return 0; } public static class Vertex{ ArrayList es = new ArrayList(); int level, p, iter; long time; void add(Vertex to, long cap) { Edge e = new Edge(to, cap), rev = new Edge(this, 0); e.rev = rev; rev.rev = e; es.add(e); to.es.add(rev); } } public static class Edge { Vertex to; Edge rev; long cap; Edge(Vertex to, long cap) { this.to = to; this.cap = cap; } } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); final int M = sc.nextInt(); final int d = sc.nextInt(); Vertex S = new Vertex(); Vertex T = new Vertex(); ArrayList> from_cities = new ArrayList>(); ArrayList> to_cities = new ArrayList>(); for(int i = 0; i < N; i++){ from_cities.add(new ArrayList()); to_cities.add(new ArrayList()); } for (int i = 0; i < M; i++) { final int u = sc.nextInt() - 1; final int v = sc.nextInt() - 1; final long p = sc.nextLong(); final long q = sc.nextLong(); final long w = sc.nextLong(); Vertex from = new Vertex(); Vertex to = new Vertex(); from.time = p; to.time = q; from.add(to, w); from_cities.get(u).add(from); to_cities.get(v).add(to); } for(final Vertex v : from_cities.get(0)){ S.add(v, INF); } for(final Vertex v : to_cities.get(N - 1)){ v.add(T, INF); } for(int i = 0; i < N; i++){ for(final Vertex from : from_cities.get(i)){ for(final Vertex to : to_cities.get(i)){ if(to.time + d <= from.time){ to.add(from, INF); } } } } System.out.println(dinic(S, T)); } public static class Scanner implements Closeable { private BufferedReader br; private StringTokenizer tok; public Scanner(InputStream is) throws IOException { br = new BufferedReader(new InputStreamReader(is)); } private void getLine() throws IOException { while (!hasNext()) { tok = new StringTokenizer(br.readLine()); } } private boolean hasNext() { return tok != null && tok.hasMoreTokens(); } public String next() throws IOException { getLine(); return tok.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public int[] nextIntArray(int n) throws IOException { final int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = this.nextInt(); } return ret; } public long[] nextLongArray(int n) throws IOException { final long[] ret = new long[n]; for (int i = 0; i < n; i++) { ret[i] = this.nextLong(); } return ret; } public void close() throws IOException { br.close(); } } }