#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
const long long p = 998244353;
long long qp(long long x, long long y) {
  long long res = 1;
  for (long long t = x; y; y >>= 1, t = t * t % p)
    if (y & 1) res = res * t % p;
  return res;
}
struct node {
  long long u, v, w;
} a[1000003];
vector<node> v[1000003];
long long f[1000003], g[1000003];
long long c[1000003];
signed main() {
  long long n = read(), m = read(), k = read() + 1;
  for (long long i = 1; i <= m; ++i) {
    long long X = read();
    a[i].u = read();
    a[i].v = read();
    a[i].w = read();
    v[X].push_back(a[i]);
  }
  long long ans = 1e18;
  memset(f, -1, sizeof(f)), memset(g, -1, sizeof(g));
  memset(c, -1, sizeof(c));
  long long S = 0, C = 0;
  for (long long i = 1; i <= 1000000; ++i) {
    for (node j : v[i])
      if (j.v == 0) {
        if (c[j.u] == -1) {
          S += j.w, ++C, c[j.u] = j.w;
        } else if (c[j.u] > j.w) {
          S -= c[j.u] - j.w, c[j.u] = j.w;
        }
      }
    if (C == n) f[i] = S;
  }
  S = 0, C = 0;
  memset(c, -1, sizeof(c));
  for (long long i = 1000000; i >= 1; --i) {
    for (node j : v[i])
      if (j.u == 0) {
        if (c[j.v] == -1) {
          S += j.w, ++C, c[j.v] = j.w;
        } else if (c[j.v] > j.w) {
          S -= c[j.v] - j.w, c[j.v] = j.w;
        }
      }
    if (C == n) g[i] = S;
  }
  for (long long i = 1; i + k <= 1000000; ++i)
    if (f[i] > -1 && g[i + k] > -1) {
      ans = min(ans, f[i] + g[i + k]);
    }
  if (ans > 1e17)
    puts("-1");
  else
    printf("%lld\n", ans);
  return 0;
}
