#include <bits/stdc++.h>
using namespace std;
inline char nc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  char ch = nc();
  int sum = 0;
  while (!(ch >= '0' && ch <= '9')) ch = nc();
  while (ch >= '0' && ch <= '9') sum = sum * 10 + ch - 48, ch = nc();
  return sum;
}
const int MAXN = (int)1e5 + 10;
const int MAXM = (int)1e6 + 10;
const long long INF = 0x3f3f3f3f3f3f3f3f;
long long f[MAXM], g[MAXM];
struct plane {
  int d, f, t, c;
  plane(int d, int f, int t, int c) : d(d), f(f), t(t), c(c) {}
  bool operator<(const plane &p2) const { return d < p2.d; }
};
vector<plane> go, back;
int gomn[MAXN], mnback[MAXN];
int n, m, k;
int mn = INF, mx;
bool vis[MAXN];
int main() {
  go.reserve(MAXM);
  back.reserve(MAXM);
  n = read(), m = read(), k = read();
  for (int i = 1; i <= m; i++) {
    int d, f, t, c;
    d = read(), f = read(), t = read(), c = read();
    if (f) {
      go.push_back(plane(d, f, t, c));
    } else {
      back.push_back(plane(d, f, t, c));
    }
    mn = min(mn, d);
    mx = max(mx, d);
  }
  sort(go.begin(), go.end());
  sort(back.begin(), back.end());
  memset(f, 0x3f, sizeof(f));
  memset(g, 0x3f, sizeof(g));
  int v = n, ptr = 0;
  long long sum = 0;
  for (int i = mn; i <= mx; i++) {
    f[i] = f[i - 1];
    for (; go[ptr].d <= i && ptr < go.size(); ptr++) {
      if (!v) {
        f[i] = min(f[i], f[i] - gomn[go[ptr].f] + go[ptr].c);
        gomn[go[ptr].f] = min(gomn[go[ptr].f], go[ptr].c);
      } else if (!vis[go[ptr].f]) {
        v--;
        sum += go[ptr].c;
        gomn[go[ptr].f] = go[ptr].c;
        vis[go[ptr].f] = true;
        if (!v) f[i] = sum;
      } else {
        sum = min(sum, sum - gomn[go[ptr].f] + go[ptr].c);
        gomn[go[ptr].f] = min(gomn[go[ptr].f], go[ptr].c);
      }
    }
  }
  if (v) {
    putchar('-');
    putchar('1');
    return 0;
  }
  sum = 0;
  v = n;
  memset(vis, false, sizeof(vis));
  ptr = back.size() - 1;
  for (int i = mx; i >= mn; i--) {
    g[i] = g[i + 1];
    for (; ptr >= 0 && back[ptr].d >= i; ptr--) {
      if (!v) {
        g[i] = min(g[i], g[i] - mnback[back[ptr].t] + back[ptr].c);
        mnback[back[ptr].t] = min(mnback[back[ptr].t], back[ptr].c);
      } else if (!vis[back[ptr].t]) {
        v--;
        vis[back[ptr].t] = true;
        sum += back[ptr].c;
        mnback[back[ptr].t] = back[ptr].c;
        if (!v) g[i] = sum;
      } else {
        sum = min(sum, sum - mnback[back[ptr].t] + back[ptr].c);
        mnback[back[ptr].t] = min(mnback[back[ptr].t], back[ptr].c);
      }
    }
  }
  if (v) {
    putchar('-');
    putchar('1');
    return 0;
  }
  long long ans = INF;
  for (int i = mn; i <= mx && i + k + 1 <= mx; i++)
    ans = min(ans, f[i] + g[i + k + 1]);
  if (ans >= INF) {
    putchar('-');
    putchar('1');
  } else {
    int a[20];
    a[0] = 0;
    while (ans) {
      a[++a[0]] = ans % 10;
      ans /= 10;
    }
    for (int i = a[0]; i >= 1; i--) {
      putchar(a[i] + '0');
    }
  }
  return 0;
}
