#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<utility>
#include<queue>
#include<string.h>
#include<string>
#include<set>
#include<iostream>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
typedef queue<P> Q;
#include<vector>
using namespace std;
const int MAX_V = 1001;
const int INF = 2000000000;
struct edge {
	int to;//行先
	int capacity;//容量
	int reverse;//逆辺
};

vector<edge> G[MAX_V*2000];
bool used[MAX_V*2000];

void AddEdge(int from, int to, int capacity) {
	G[from].push_back(edge{ to, capacity, (int)G[to].size() });
	G[to].push_back(edge{ from,0,(int)G[from].size() - 1 });
}

LL dfs(int v, int t, LL f) {
	if (v == t) {
		return f;
	}
	used[v] = true;
	for (int i = 0; i < G[v].size(); i++) {
		edge &e = G[v][i];
		if (!used[e.to] && e.capacity > 0) {//使える辺を見つけたら
			LL d = dfs(e.to, t, min(f, (LL)e.capacity));
			if (d>0) {
				e.capacity -= d;
				G[e.to][e.reverse].capacity += d;
				return d;
			}
		}
	}
	return 0;
}

LL max_flow(int s, int t) {
	int flow = 0;
	while (true) {
		memset(used, 0, sizeof(used));
		LL f = dfs(s, t, INF);
		if (f == 0) {
			return flow;
		}
		else {
			flow += f;
		}
	}
}

#define _CRT_SECURE_NO_WARNINGS

int N,M,d = 0;
int u[MAX_V], v[MAX_V], p[MAX_V], q[MAX_V], w[MAX_V];

const int MAX_DEG = 1001;
int outdeg[MAX_V] = {};
int startTime[MAX_V][MAX_DEG];

int indeg[MAX_V] = {};
int goalTime[MAX_V][MAX_DEG];

int main() {
	cin >> N >> M >> d;
	for (int i = 0; i < M;i++) {
		cin >> u[i] >> v[i] >> p[i] >> q[i] >> w[i];
		AddEdge(u[i] * 2000 + 1000+outdeg[u[i]], v[i] * 2000 + indeg[v[i]], w[i]);
		startTime[ u[i] ][ outdeg[u[i]] ] = p[i];
		goalTime[v[i]][indeg[v[i]]] = q[i];
		outdeg[u[i]]++;
		indeg[v[i]]++;
	}
	for (int i = 1; i <= N;i++) {
		for (int j = 0; j < indeg[i];j++) {
			for (int k = 0; k < outdeg[i];k++) {
				if ( goalTime[i][j]+d <= startTime[i][k] ) {
					AddEdge(i * 2000 + j, i * 2000+1000 + k, INF);
				}
			}
		}
	}
	for (int i = 0; i < outdeg[1];i++) {
		AddEdge(0, 2000 +1000+ i, INF);
			}
	for (int i = 0; i < indeg[N];i++) {
		AddEdge(N * 2000 + i, 1, INF);
	}
	cout << max_flow(0, 1) << endl;
	return 0;
}