#include<bits/stdc++.h>
#define N 200010
using namespace std;
int n,m,k,i,x,y,h[N],t;
long long ans,Ans,g[N],f[N];
struct mjj{int x,y,z;}a[N];
bool cmp(mjj a,mjj b){return a.x<b.x;}
int main() {
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=m;i++){
scanf("%d%d%d%d",&a[i].x,&x,&y,&a[i].z);
if(x==0)a[i].y=y;
else a[i].y=-x;
}
Ans=0;
Ans=1e18;
sort(a+1,a+m+1,cmp);
for(i=1;i<=m;i++)h[i]=a[i].x;
for(i=1;i<=n;i++)f[i]=1e12,ans+=f[i];
g[0]=1e18;
for(i=1;i<=m;i++)if(a[i].y<0){
ans-=f[-a[i].y];
f[-a[i].y]=min(f[-a[i].y],1ll*a[i].z);
ans+=f[-a[i].y];
g[i]=ans;
}else g[i]=g[i-1];
memset(f,63,sizeof(f));
ans=0;
for(i=1;i<=n;i++)f[i]=1e12,ans+=f[i];
for(i=m;i>=1;i--)if(a[i].y>0){
ans-=f[a[i].y];
f[a[i].y]=min(f[a[i].y],1ll*a[i].z);
ans+=f[a[i].y];
t=lower_bound(h+1,h+m+1,a[i].x-k)-h-1;
if(t)Ans=min(Ans,ans+g[t]);
}
if(Ans>=1e12)puts("-1");
else printf("%I64d\n",Ans);
}
1528B - Kavi on Pairing Duty | 339B - Xenia and Ringroad |
189A - Cut Ribbon | 1182A - Filling Shapes |
82A - Double Cola | 45A - Codecraft III |
1242A - Tile Painting | 1663E - Are You Safe |
1663D - Is it rated - 3 | 1311A - Add Odd or Subtract Even |
977F - Consecutive Subsequence | 939A - Love Triangle |
755A - PolandBall and Hypothesis | 760B - Frodo and pillows |
1006A - Adjacent Replacements | 1195C - Basketball Exercise |
1206A - Choose Two Numbers | 1438B - Valerii Against Everyone |
822A - I'm bored with life | 9A - Die Roll |
1430B - Barrels | 279B - Books |
1374B - Multiply by 2 divide by 6 | 1093B - Letters Rearranging |
1213C - Book Reading | 1468C - Berpizza |
1546B - AquaMoon and Stolen String | 1353C - Board Moves |
902A - Visiting a Friend | 299B - Ksusha the Squirrel |