#include <bits/stdc++.h>   using namespace std;   const int N = 1E5 + 1;   long long int n, m, rk, ans;   set<pair<long long int, long long int> > dep[N], dpt;   vector<pair<long long int, pair<long long int, long long int> > > query;   pair<long long int, long long int> arrival[N];   long long int setarrival() {     for (long long int i = 0; i <= n; i++) {       arrival[i].first = -1;       arrival[i].second = 1E17;     }     long long int i = 0;     set<long long int> s;     for (i = 0; i < query.size(); i++) {       if (s.size() < n) {         long long int c1 = query[i].second.second;         long long int f = query[i].second.first;         if (arrival[f].second > c1) {           arrival[f].second = c1;           arrival[f].first = query[i].first;         }         s.insert(f);       } else         break;     }     if (s.size() < n) return -1;     return i;   }   long long int setd() {     for (long long int i = 1; i <= n; i++) {       if (dep[i].empty()) return -1;       set<pair<long long int, long long int> >::iterator it = dep[i].begin();       long long int c = (*it).first;       long long int d = (*it).second;       ans += c;       dpt.insert({d, i});     }     return 1;   }   long long int check() {     long long int mx = 0;     for (long long int i = 1; i <= n; i++) {       long long int d = arrival[i].first;       ans += arrival[i].second;       if (d == -1) return -1;       mx = max(d, mx);     }     return mx;   }   long long int setdept(long long int day) {     long long int rd = day + rk + 1;     while (1) {       set<pair<long long int, long long int> >::iterator k = dpt.begin();       long long int d = (*k).first;       long long int i = (*k).second;       if (d >= rd) break;       long long int f = 0, in = 0;       while (!dep[i].empty()) {         set<pair<long long int, long long int> >::iterator it = dep[i].begin();         long long int c = (*it).first;         d = (*it).second;         if (d < rd) {           if (in == 0) ans -= c;           in++;           dep[i].erase(*dep[i].begin());         } else {           f = 1;           ans += c;           dpt.erase(k);           dpt.insert({d, i});           break;         }       }       if (f == 0) return -1;     }     return 1;   }   int main() {     cin >> n >> m >> rk;     vector<long long int> v;     while (m--) {       long long int d, f, t, c;       cin >> d >> f >> t >> c;       if (f == 0)         dep[t].insert({c, d});       else         query.push_back({d, {f, c}});     }     sort(query.begin(), query.end());     ans = 0;     long long int idx = setarrival();     long long int ik = setd();     if (idx == -1 || ik == -1) {     REP:       cout << -1;       return 0;     }     long long int day = check();     if (day == -1) goto REP;     long long int sd = setdept(day);     if (sd == -1) goto REP;     v.push_back(ans);     for (long long int i = idx; i < query.size(); i++) {       long long int f = query[i].second.first;       long long int d = query[i].first;       long long int c = query[i].second.second;       if (arrival[f].second > c) {         ans = v.back();         ans -= arrival[f].second;         ans += c;         sd = setdept(d);         if (sd == -1) break;         v.push_back(ans);         arrival[f].second = c;         arrival[f].first = d;       }     }     ans = *min_element(v.begin(), v.end());     cout << ans;     return 0;   }