#include <bits/stdc++.h>
using namespace std;
int n, k, m;
vector<int> start[1000000 + 5], finish[1000000 + 5];
pair<int, int> cp[200005];
long long ans;
long long cores[1000000 + 5], cost[1000000 + 5];
void update(int c, int p, int i) {
  for (; i <= 1000000; i += i & -i) {
    cores[i] += c;
    cost[i] += (long long)p * c;
  }
}
long long queryCores(int i) {
  long long res = 0;
  for (; i > 0; i -= i & -i) res += cores[i];
  return res;
}
long long queryCost(int i) {
  long long res = 0;
  for (; i > 0; i -= i & -i) res += cost[i];
  return res;
}
int main() {
  scanf("%d%d%d", &n, &k, &m);
  for (int i = 0; i < m; i++) {
    int l, r, c, p;
    scanf("%d%d%d%d", &l, &r, &c, &p);
    start[l].push_back(i);
    finish[r].push_back(i);
    cp[i] = pair<int, int>(c, p);
  }
  for (int i = 1; i <= n; i++) {
    for (int j : start[i]) {
      int c, p;
      tie(c, p) = cp[j];
      update(c, p, p);
    }
    if (queryCores(1000000) <= k) {
      ans += queryCost(1000000);
    } else {
      int maxp = -1;
      int lo = 1, hi = 1000000, mid;
      while (lo <= hi) {
        mid = (lo + hi) / 2;
        if (queryCores(mid) >= k) {
          hi = mid - 1;
          maxp = mid;
        } else {
          lo = mid + 1;
        }
      }
      long long curcost = queryCost(maxp);
      int extra = queryCores(maxp) - k;
      curcost -= (long long)extra * maxp;
      ans += curcost;
    }
    for (int j : finish[i]) {
      int c, p;
      tie(c, p) = cp[j];
      update(-c, p, p);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
