#include <bits/stdc++.h>
using namespace std;
const int64_t maxn = 1.5e6;
vector<int64_t> qui[maxn], quo[maxn];
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int64_t n, m, k;
  cin >> n >> m >> k;
  int64_t d[m], f[m], t[m], c[m];
  set<pair<int64_t, int64_t>> in[n + 1], out[n + 1];
  for (int64_t i = 0; i < m; i++) {
    cin >> d[i] >> f[i] >> t[i] >> c[i];
    if (f[i] != 0 && t[i] != 0) continue;
    if (t[i] == 0)
      qui[d[i]].push_back(i);
    else if (d[i] >= k)
      out[t[i]].insert({c[i], d[i]}), quo[d[i]].push_back(i);
  }
  int64_t ans = 1e18;
  int64_t sumo = 0, sumi = 0;
  int64_t empty_o = 0;
  int64_t empty_i = n;
  for (int64_t i = 1; i <= n; i++) {
    if (out[i].empty())
      empty_o++;
    else
      sumo += out[i].begin()->first;
  }
  for (int64_t i = 0; i < maxn; i++) {
    if (i + k - 1 < maxn)
      for (auto it : quo[i + k - 1]) {
        sumo -= out[t[it]].begin()->first;
        out[t[it]].erase({c[it], d[it]});
        if (out[t[it]].empty())
          empty_o++;
        else
          sumo += out[t[it]].begin()->first;
      }
    if (empty_o == 0 && empty_i == 0) ans = min(ans, sumo + sumi);
    for (auto it : qui[i]) {
      if (in[f[it]].empty())
        empty_i--;
      else
        sumi -= in[f[it]].begin()->first;
      in[f[it]].insert({c[it], d[it]});
      sumi += in[f[it]].begin()->first;
    }
  }
  if (ans == 1e18)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
