#include <bits/stdc++.h>
using namespace std;
long long add(long long a, long long b) {
  a += b;
  if (a >= 1000000007) a -= 1000000007;
  return a;
}
long long mul(long long a, long long b) {
  return ((a % 1000000007) * (b % 1000000007)) % 1000000007;
}
long long binary_expo(long long base, long long expo) {
  long long res = 1;
  while (expo) {
    if (expo % 2) {
      res = mul(res, base);
      expo /= 2;
    }
    base = mul(base, base);
  }
  return res;
}
vector<pair<long long, pair<long long, long long> > > v[1000002];
const long long N = 100002;
long long Arr[N], dep[N];
multiset<long long> S1[N], S2[N];
long long n;
long long BIT1[N], BIT2[N];
void update(long long BIT[], long long idx, long long val) {
  while (idx <= n) {
    BIT[idx] += val;
    idx += (idx & (-idx));
  }
}
long long query(long long BIT[], long long idx) {
  long long sum = 0;
  while (idx > 0) {
    sum += BIT[idx];
    idx -= (idx & (-idx));
  }
  return sum;
}
void solve() {
  long long m, k;
  cin >> n >> m >> k;
  for (long long i = 1; i <= n; i++) {
    Arr[i] = 1e12;
    dep[i] = -1e12;
  }
  for (long long i = 0; i < m; i++) {
    long long d, f, t, c;
    cin >> d >> f >> t >> c;
    v[d].push_back({f, {t, c}});
    if (t == 0) {
      Arr[f] = min(Arr[f], d);
    } else {
      dep[t] = max(dep[t], d);
    }
  }
  long long minn = 1e12;
  long long maxx = -1e12;
  for (long long i = 1; i <= n; i++) {
    if (Arr[i] == 1e12 or dep[i] == -1e12) {
      cout << "-1" << endl;
      return;
    }
    maxx = max(maxx, Arr[i]);
    minn = min(minn, dep[i]);
  }
  long long ms = maxx + 1;
  long long me = minn - 1;
  if (me - ms + 1 < k) {
    cout << "-1" << endl;
    return;
  }
  for (long long i = 1000000; i > (ms + k - 1); i--) {
    for (long long j = 0; j < v[i].size(); j++) {
      pair<long long, pair<long long, long long> > p = v[i][j];
      if (p.first == 0) {
        long long val1 = 0;
        if (S1[p.second.first].size() != 0) {
          val1 = *(S1[p.second.first].begin());
        }
        S1[p.second.first].insert(p.second.second);
        long long val = *(S1[p.second.first].begin());
        update(BIT2, p.second.first, val - val1);
      }
    }
  }
  for (long long i = 1; i < ms; i++) {
    for (long long j = 0; j < v[i].size(); j++) {
      pair<long long, pair<long long, long long> > p = v[i][j];
      if (p.second.first == 0) {
        long long val1 = 0;
        if (S2[p.first].size() != 0) {
          val1 = *(S2[p.first].begin());
        }
        S2[p.first].insert(p.second.second);
        long long val = *(S2[p.first].begin());
        update(BIT1, p.first, val - val1);
      }
    }
  }
  long long ans = query(BIT1, n) + query(BIT2, n);
  for (long long i = ms + k; i <= me; i++) {
    long long ar = i - k;
    long long de = i;
    for (long long j = 0; j < v[i].size(); j++) {
      pair<long long, pair<long long, long long> > p = v[i][j];
      if (p.first == 0) {
        long long val1 = 0;
        if (S1[p.second.first].size() != 0) {
          val1 = *(S1[p.second.first].begin());
        }
        S1[p.second.first].erase(S1[p.second.first].find(p.second.second));
        long long val = *(S1[p.second.first].begin());
        update(BIT2, p.second.first, val - val1);
      }
    }
    for (long long j = 0; j < v[ar].size(); j++) {
      pair<long long, pair<long long, long long> > p = v[ar][j];
      if (p.second.first == 0) {
        long long val1 = 0;
        if (S2[p.first].size() != 0) {
          val1 = *(S2[p.first].begin());
        }
        S2[p.first].insert(p.second.second);
        long long val = *(S2[p.first].begin());
        update(BIT1, p.first, val - val1);
      }
    }
    ans = min(ans, query(BIT1, n) + query(BIT2, n));
  }
  cout << ans << endl;
}
signed main() {
  long long t;
  ios_base::sync_with_stdio(false);
  t = 1;
  while (t--) solve();
  return 0;
}
