#include <bits/stdc++.h>
using namespace std;
int n, k, m, l, r, mid;
long long bit1[1000005], bit2[1000005], ans, num[1000005], last;
vector<pair<int, int> > a[1000005], b[1000005];
bool flag[1000005];
int lowbit(int x) { return x & -x; }
void add1(int x, int y) {
  while (x <= 1000000) {
    bit1[x] += y;
    x += lowbit(x);
  }
}
void add2(int x, long long y) {
  while (x <= 1000000) {
    bit2[x] += y;
    x += lowbit(x);
  }
}
long long query1(int x) {
  long long ans = 0;
  while (x) {
    ans += bit1[x];
    x -= lowbit(x);
  }
  return ans;
}
long long query2(int x) {
  long long ans = 0;
  while (x) {
    ans += bit2[x];
    x -= lowbit(x);
  }
  return ans;
}
int main() {
  scanf("%d%d%d", &n, &k, &m);
  for (int i = 1; i <= m; i++) {
    int l, r, c, p;
    scanf("%d%d%d%d", &l, &r, &c, &p);
    a[l].push_back(make_pair(c, p));
    b[r + 1].push_back(make_pair(-c, p));
    flag[l] = true;
    flag[r + 1] = true;
  }
  for (int i = 1; i <= n; i++) {
    for (auto j : a[i]) {
      add1(j.second, j.first);
      add2(j.second, (long long)j.second * j.first);
      num[j.second] += j.first;
    }
    for (auto j : b[i]) {
      add1(j.second, j.first);
      add2(j.second, (long long)j.second * j.first);
      num[j.second] += j.first;
    }
    if (!flag[i]) {
      ans += last;
      continue;
    }
    l = 1;
    r = 1000000;
    while (l < r) {
      mid = l + r >> 1;
      if (query1(mid) >= (long long)k)
        r = mid;
      else
        l = mid + 1;
    }
    last = query2(r - 1);
    long long x = min(k - query1(r - 1), num[r]);
    last += x * r;
    ans += last;
  }
  printf("%lld\n", ans);
  return 0;
}
