#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:268435456")
using namespace std;
template <typename T>
inline T abs(T a) {
  return ((a < 0) ? -a : a);
}
template <typename T>
inline T sqr(T a) {
  return a * a;
}
template <class T>
T gcd(T a, T b) {
  return a ? gcd(b % a, a) : b;
}
template <class T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <class T>
T sign(T a) {
  return a > 0 ? 1 : (a < 0 ? -1 : 0);
}
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const int dxK[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dyK[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dxKn[] = {-2, -1, 1, 2, 2, 1, -1, -2};
const int dyKn[] = {1, 2, 2, 1, -1, -2, -2, -1};
const int N = int(1000 * 1000) + 5;
const int M = int(10000) + 9;
const int LOGN = 21;
const int SQN = 350;
const int MOD = int(1e9) + 7;
const int INF = int(1e9) + 100;
const long long INF64 = 2e18;
const double PI = double(3.1415926535897932384626433832795);
const double e = double(2.7182818284590452353602874713527);
const double EPS = 1e-9;
struct wtf {
  int v, to, c;
  wtf() {}
  wtf(int v, int to, int c) : v(v), to(to), c(c) {}
};
int n, m, k;
vector<wtf> a[N];
multiset<int> qin[N], qout[N];
int cntin, cntout;
long long sum;
void addin(int t) {
  if (t >= N) return;
  for (int i = 0; i < (int)((int)(a[t].size())); ++i) {
    wtf cur = a[t][i];
    if (cur.to != -1) continue;
    int v = cur.v;
    if ((int)(qin[v].size()) == 0) {
      ++cntin;
      sum += cur.c;
      qin[v].insert(cur.c);
    } else {
      sum -= *qin[v].begin();
      qin[v].insert(cur.c);
      sum += *qin[v].begin();
    }
  }
}
void addout(int t) {
  if (t >= N) return;
  for (int i = 0; i < (int)((int)(a[t].size())); ++i) {
    wtf cur = a[t][i];
    if (cur.v != -1) continue;
    int v = cur.to;
    if ((int)(qout[v].size()) == 0) {
      ++cntout;
      sum += cur.c;
      qout[v].insert(cur.c);
    } else {
      sum -= *qout[v].begin();
      qout[v].insert(cur.c);
      sum += *qout[v].begin();
    }
  }
}
void delout(int t) {
  if (t >= N) return;
  for (int i = 0; i < (int)((int)(a[t].size())); ++i) {
    wtf cur = a[t][i];
    if (cur.v != -1) continue;
    int v = cur.to;
    if ((int)(qout[v].size()) == 1) {
      assert(qout[v].count(cur.c) != 0);
      --cntout;
      sum -= cur.c;
      qout[v].erase(cur.c);
    } else {
      sum -= *qout[v].begin();
      qout[v].erase(qout[v].find(cur.c));
      sum += *qout[v].begin();
    }
  }
}
void solve() {
  cin >> n >> m >> k;
  for (int i = 0; i < (int)(m); ++i) {
    int d, v, to, c;
    scanf("%d %d %d %d", &d, &v, &to, &c);
    --v, --to;
    a[d].push_back(wtf(v, to, c));
  }
  long long res = INF64;
  addin(0);
  for (int i = k + 1; i < N; ++i) addout(i);
  for (int i = 1; i < N; ++i) {
    if (cntin == cntout && cntin == n) res = min(res, sum);
    addin(i);
    delout(i + k);
  }
  if (res == INF64) res = -1;
  cout << res << endl;
}
int main() {
  srand(time(NULL));
  cout << setprecision(25) << fixed;
  cerr << setprecision(25) << fixed;
  solve();
  return 0;
}
