#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   bool isin(       const pair<long long, tuple<long long, long long, long long> >& elem) {     return get<1>(elem.second) == 0;   }   bool isout(       const pair<long long, tuple<long long, long long, long long> >& elem) {     return get<0>(elem.second) == 0;   }   long long INF = 1e18;   int main() {     long long n, m, k;     cin >> n >> m >> k;     if (m == 0) {       cout << -1;       return 0;     }     vector<pair<long long, tuple<long long, long long, long long> > > flights;     long long d, f, t, c;     for (long long i = 0; i < m; i++) {       cin >> d >> f >> t >> c;       flights.push_back(           make_pair(d, tuple<long long, long long, long long>(f, t, c)));     }     sort(flights.begin(), flights.end());     long long days_cnt = flights.back().first + 2;     vector<bool> in(n + 1, false);     vector<bool> out(n + 1, false);     vector<long long> totalin(days_cnt, 0);     vector<long long> totalout(days_cnt, 0);     vector<long long> pre_min(days_cnt, INF);     vector<long long> suf_min(days_cnt, INF);     vector<long long> p_min(n + 1, INF);     vector<long long> p_min_rev(n + 1, INF);     long long cur = 0;     for (long long i = 0; i < flights.size(); i++) {       auto elem = flights[i];       long long d = elem.first;       long long f = get<0>(elem.second);       long long t = get<1>(elem.second);       long long c = get<2>(elem.second);       long long p = f;       if (p == 0) {         continue;       }       while (cur < d) {         cur++;         totalin[cur] = totalin[cur - 1];         pre_min[cur] = pre_min[cur - 1];       }       if (!in[p]) {         totalin[d]++;         in[p] = true;         p_min[p] = c;         pre_min[d] = (pre_min[d] == INF ? 0 : pre_min[d]) + p_min[p];       } else {         long long old_min = p_min[p];         p_min[p] = min(p_min[p], c);         if (old_min > p_min[p]) {           pre_min[d] -= old_min;           pre_min[d] += p_min[p];         }       }     }     while (cur < days_cnt) {       cur++;       if (cur == days_cnt) continue;       pre_min[cur] = pre_min[cur - 1];       totalin[cur] = totalin[cur - 1];     }     cur = days_cnt - 1;     for (long long i = flights.size() - 1; i >= 0; i--) {       auto elem = flights[i];       long long d = elem.first;       long long f = get<0>(elem.second);       long long t = get<1>(elem.second);       long long c = get<2>(elem.second);       long long p = t;       if (p == 0) {         continue;       }       while (cur > d) {         cur--;         totalout[cur] = totalout[cur + 1];         suf_min[cur] = suf_min[cur + 1];       }       if (!out[p]) {         totalout[d]++;         out[p] = true;         p_min_rev[p] = c;         suf_min[d] = (suf_min[d] == INF ? 0 : suf_min[d]) + p_min_rev[p];       } else {         long long old_min = p_min_rev[p];         p_min_rev[p] = min(p_min_rev[p], c);         if (old_min > p_min_rev[p]) {           suf_min[d] -= old_min;           suf_min[d] += p_min_rev[p];         }       }     }     while (cur >= 0) {       cur--;       if (cur < 0) continue;       suf_min[cur] = suf_min[cur + 1];       totalout[cur] = totalout[cur + 1];     }     long long ans = INF;     for (long long i = 0; i < suf_min.size(); i++) {       if (i + k + 1 >= totalout.size()) continue;       if (totalin[i] >= n && totalout[i + k + 1] >= n) {         ans = min(ans, pre_min[i] + suf_min[i + k + 1]);       }     }     cout << (ans >= INF ? -1 : ans);     return 0;   }