#include <bits/stdc++.h>
using namespace std;
long long int cons;
long long int check(long long int a) {
  if (a >= cons || a <= -cons) a %= cons;
  return a;
}
long long int check2(long long int a) {
  if (a > 0) return a;
  long long int b = a / cons;
  a -= b * cons;
  if (a < 0) a += cons;
  return a;
}
long long int GCD(long long int a, long long int b) {
  if (b == 0) return a;
  return GCD(b, a % b);
}
long long int exp(long long int a, long long int n) {
  if (n == 0) return 1;
  if (n == 1) return check(a);
  long long int b = exp(a, n / 2);
  if (n % 2 == 0) return check(b * b);
  return check(b * check(b * a));
}
vector<pair<long long int, long long int> > arrive[1000000 + 2],
    depart[1000000 + 2];
vector<pair<long long int, long long int> > store_min[100000 + 1];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cons = 1e9 + 7;
  int n, m, k;
  cin >> n >> m >> k;
  vector<long long int> arrive_min, depart_min;
  vector<int> cur_ind;
  cur_ind.resize(n + 1);
  arrive_min.resize(n + 1);
  depart_min.resize(n + 1);
  for (int i = 1; i <= n; i++)
    arrive_min[i] = -1, cur_ind[i] = 0, depart_min[i] = 0;
  for (int i = 0; i < m; i++) {
    int first, second, z, w;
    cin >> first >> second >> z >> w;
    if (second == 0) {
      depart[first].push_back({z, w});
      store_min[z].push_back({w, first});
    } else {
      arrive[first].push_back({second, w});
    }
  }
  for (int i = 1; i <= n; i++) sort(store_min[i].begin(), store_min[i].end());
  int l = 1, r = k;
  int cur_counter = 0;
  long long int cur_cost = 0;
  long long int ans = -1;
  bool flag = true;
  while (flag && r <= 1e6 + 1) {
    for (auto itr : depart[r]) {
      if (arrive_min[itr.first] != -1) {
        int len = store_min[itr.first].size();
        while (cur_ind[itr.first] < len &&
               (store_min[itr.first][cur_ind[itr.first]].second) <= r)
          cur_ind[itr.first]++;
        if (cur_ind[itr.first] == len) {
          flag = false;
          break;
        } else {
          cur_cost -= depart_min[itr.first];
          depart_min[itr.first] =
              store_min[itr.first][cur_ind[itr.first]].first;
          cur_cost += depart_min[itr.first];
        }
      }
    }
    if (!flag) break;
    if (cur_counter == n) {
      if (ans == -1)
        ans = cur_cost;
      else
        ans = min(ans, cur_cost);
    }
    for (auto itr : arrive[l]) {
      if (arrive_min[itr.first] == -1) {
        arrive_min[itr.first] = itr.second;
        cur_counter++;
        cur_cost += itr.second;
        int len = store_min[itr.first].size();
        while (cur_ind[itr.first] < len &&
               (store_min[itr.first][cur_ind[itr.first]].second) <= r)
          cur_ind[itr.first]++;
        if (cur_ind[itr.first] == len) {
          flag = false;
          break;
        } else {
          cur_cost -= depart_min[itr.first];
          depart_min[itr.first] =
              store_min[itr.first][cur_ind[itr.first]].first;
          cur_cost += depart_min[itr.first];
        }
      } else {
        cur_cost -= arrive_min[itr.first];
        arrive_min[itr.first] = min(arrive_min[itr.first], itr.second);
        cur_cost += arrive_min[itr.first];
      }
    }
    l++;
    r++;
  }
  cout << ans << endl;
  return 0;
}
