#include <bits/stdc++.h>
using namespace std;
vector<int> starts[1000005];
vector<int> en[1000005];
int cores[200005];
long long used[200005];
int price[200005];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k, m;
  cin >> n >> k >> m;
  for (int j = 1; j <= m; j++) {
    int l, r;
    cin >> l >> r >> cores[j] >> price[j];
    starts[l].push_back(j);
    en[r + 1].push_back(j);
  }
  set<array<int, 2>> use, can;
  long long ats = 0;
  long long costForNow = 0;
  int coresInUse = 0;
  for (int i = 1; i <= n; i++) {
    for (auto& idx : starts[i]) can.insert({price[idx], idx});
    for (auto& idx : en[i]) {
      can.erase({price[idx], idx});
      if (use.count({price[idx], idx})) {
        use.erase({price[idx], idx});
        coresInUse -= used[idx];
        costForNow -= used[idx] * price[idx];
      }
    }
    while (!can.empty() && coresInUse < k) {
      int idx = (*can.begin())[1];
      long long canUse = min(k - coresInUse + 0LL, cores[idx] - used[idx]);
      coresInUse += canUse;
      costForNow += price[idx] * canUse;
      used[idx] += canUse;
      if (used[idx] == cores[idx]) can.erase({price[idx], idx});
      use.insert({price[idx], idx});
    }
    while (!can.empty() && !use.empty() &&
           (*use.rbegin())[0] > (*can.begin())[0]) {
      int nw = (*can.begin())[1];
      int old = (*use.rbegin())[1];
      long long canSwap = min(cores[nw] - used[nw], used[old]);
      costForNow -= price[old] * canSwap;
      costForNow += price[nw] * canSwap;
      used[nw] += canSwap;
      used[old] -= canSwap;
      can.insert({price[old], old});
      use.insert({price[nw], nw});
      if (used[nw] == cores[nw]) can.erase({price[nw], nw});
      if (used[old] == 0) use.erase({price[old], old});
    }
    ats += costForNow;
  }
  cout << ats;
}
