#include <bits/stdc++.h>
using namespace std;
long long expire[1000006];
long long core[1000006], price[1000006];
vector<pair<long long, long long> > vec;
int main() {
  int n, k, m, l, r, c, p;
  scanf("%d %d %d", &n, &k, &m);
  for (int i = 0; i < m; i++)
    scanf("%d %d %d %d", &l, &r, &c, &p), vec.push_back({l, i}),
        expire[i] = r, core[i] = c, price[i] = p;
  sort(vec.begin(), vec.end());
  set<pair<long long, long long> > cur, rem, alive;
  long long tot = 0, amount = 0, cst = 0;
  for (int i = 1, j = 0; i <= n; i++) {
    while (j < vec.size() && vec[j].first == i) {
      int ind = vec[j].second;
      rem.insert({price[ind], ind});
      alive.insert({expire[ind], ind});
      j++;
    }
    while (!cur.empty() && !rem.empty() &&
           (*cur.rbegin()).first > (*rem.begin()).first) {
      pair<long long, long long> a = *cur.rbegin();
      cur.erase(a);
      amount -= core[a.second];
      cst -= core[a.second] * price[a.second];
      pair<long long, long long> b = *rem.begin();
      rem.erase(b);
      cur.insert(b);
      rem.insert(a);
      amount += core[b.second];
      cst += core[b.second] * price[b.second];
    }
    while (!cur.empty() && amount > k) {
      pair<long long, long long> a = *cur.rbegin();
      cur.erase(a);
      rem.insert(a);
      amount -= core[a.second];
      cst -= core[a.second] * price[a.second];
    }
    while (!rem.empty() && amount < k) {
      pair<long long, long long> a = *rem.begin();
      rem.erase(a);
      cur.insert(a);
      amount += core[a.second];
      cst += core[a.second] * price[a.second];
    }
    tot += cst;
    if (amount > k) tot -= (amount - k) * (*cur.rbegin()).first;
    while (!alive.empty() && (*alive.begin()).first == i) {
      pair<long long, long long> p = *alive.begin();
      alive.erase(p);
      p = {price[p.second], p.second};
      if (rem.find(p) != rem.end()) rem.erase(p);
      if (cur.find(p) != cur.end()) {
        cur.erase(p);
        int ind = p.second;
        amount -= core[ind];
        cst -= core[ind] * price[ind];
      }
    }
  }
  printf("%lld\n", tot);
  return 0;
}
