#include //ios::sync_with_stdio(false);cin.tie(0); using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair pii; typedef pair ppii; typedef pair pipi; typedef pair pll; typedef pair ppll; typedef pair plpl; typedef tuple tl; ll mod=1000000007; ll mod2=998244353; ll mod3=1000003; ll mod4=998244853; ll inf=1000000000000000000; double pi=2*acos(0); #define rep(i,m,n) for(ll i=m;i=m;i--) int dh[4]={1,-1,0,0}; int dw[4]={0,0,1,-1}; int ddh[8]={-1,-1,-1,0,0,1,1,1}; int ddw[8]={-1,0,1,-1,1,-1,0,1}; ll lmax(ll a,ll b){ if(a0){ if(k&1)ret*=now; now*=now; k/=2; } return ret; } struct edge{ll to,cap,rev,st,go;}; vector G[4000010]; bool used[4000010]; void add_edge(ll from,ll to,ll cap,ll st,ll go){ G[from].push_back((edge){to,cap,(ll)G[to].size(),st,go}); G[to].push_back((edge){from,0,(ll)G[from].size()-1,0,1000000000000}); } ll dfs(ll v,ll t,ll f,ll ss){ if(v==t)return f; used[v]=true; rep(i,0,G[v].size()){ edge &e=G[v][i]; if(e.cap>0&&e.st>=ss){ ll d=dfs(e.to,t,min(f,e.cap),e.go); if(d>0){ e.cap-=d; G[e.to][e.rev].cap+=d; return d; } } } return 0; } ll max_flow(ll s,ll t){ ll f=0; for(;;){ memset(used,0,sizeof used); ll g=dfs(s,t,1000000000,0); if(g==0)return f; f+=g; } } int main(){ ios::sync_with_stdio(false);cin.tie(0); ll n,m,d;cin>>n>>m>>d; rep(i,0,m){ ll u,v,p,q,w;cin>>u>>v>>p>>q>>w; add_edge(u,v,w,p,q+d); } ll ans=max_flow(1,n); cout<