#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
const long long inf = 1000000000000000000;
const long long KOK = 100000;
const long long LOG = 30;
const long long li = 1000005;
const long long mod = 1000000007;
long long n, m, b[li], a[li], k, flag, t, tree[li * 4], tree1[li * 4];
long long ans;
string s;
priority_queue<pair<long long, pair<long long, long long>>> ind;
pair<long long, pair<long long, pair<long long, long long>>> p[li];
inline void update(long long node, long long start, long long end, long long l,
                   long long r, long long cntt, long long val) {
  if (start > end || start > r || end < l) return;
  if (start >= l && end <= r) {
    tree[node] += cntt;
    tree1[node] += val * cntt;
    return;
  }
  update(node * 2, start, ((start + end) / 2), l, r, cntt, val),
      update(node * 2 + 1, ((start + end) / 2) + 1, end, l, r, cntt, val);
  tree[node] = tree[node * 2] + tree[node * 2 + 1];
  tree1[node] = tree1[node * 2] + tree1[node * 2 + 1];
}
inline long long query(long long node, long long start, long long end,
                       long long l, long long r, long long ty) {
  if (start > end || start > r || end < l) return 0;
  if (start >= l && end <= r) {
    if (ty == 0) return tree[node];
    if (ty == 1) return tree1[node];
  }
  return query(node * 2, start, ((start + end) / 2), l, r, ty) +
         query(node * 2 + 1, ((start + end) / 2) + 1, end, l, r, ty);
}
int32_t main(void) {
  scanf("%lld %lld %lld", &n, &k, &m);
  for (long long i = 1; i <= m; i++) {
    scanf("%lld %lld %lld %lld", &p[i].first, &p[i].second.first,
          &p[i].second.second.first, &p[i].second.second.second);
  }
  sort(p + 1, p + m + 1);
  long long bass = 1;
  for (long long i = 1; i <= n; i++) {
    while (bass <= m && p[bass].first == i) {
      update(1, 1, 1000000, p[bass].second.second.second,
             p[bass].second.second.second, p[bass].second.second.first,
             p[bass].second.second.second);
      ind.push({-p[bass].second.first,
                {p[bass].second.second.first, p[bass].second.second.second}});
      bass++;
    }
    long long bas = 1;
    long long son = 1000000;
    while (bas <= son) {
      if (query(1, 1, 1000000, 1, ((bas + son) / 2), 0) >= k) {
        son = ((bas + son) / 2) - 1;
      } else
        bas = ((bas + son) / 2) + 1;
    }
    if (bas > 1000000) {
      ans += query(1, 1, 1000000, 1, 1000000, 1);
    } else {
      ans += query(1, 1, 1000000, 1, bas, 1);
      ans -= (query(1, 1, 1000000, 1, bas, 0) - k) * bas;
    }
    while (ind.size() && ind.top().first == -i) {
      update(1, 1, 1000000, ind.top().second.second, ind.top().second.second,
             -ind.top().second.first, ind.top().second.second);
      ind.pop();
    }
  }
  printf("%lld\n", ans);
  return 0;
}
