#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100000;
long long n, k, m;
long long mi[maxn << 2], tag[maxn << 2], query, c[maxn];
struct plan {
  long long l, r, c, p;
} p[maxn];
void add(int x, long long y) {
  for (int i = x; i <= n; i += i & (-i)) c[i] += y;
}
long long getsum(int x) {
  long long ans = 0;
  for (int i = x; i; i -= i & (-i)) ans += c[i];
  return ans;
}
void build(int l, int r, int rt) {
  if (l == r) {
    mi[rt] = k;
    return;
  }
  int m = l + r >> 1;
  build(l, m, rt << 1);
  build(m + 1, r, rt << 1 | 1);
  mi[rt] = min(mi[rt << 1], mi[rt << 1 | 1]);
}
void down(int rt) {
  tag[rt << 1] += tag[rt];
  tag[rt << 1 | 1] += tag[rt];
  mi[rt << 1] -= tag[rt];
  mi[rt << 1 | 1] -= tag[rt];
  tag[rt] = 0;
}
void modify(int l, int r, int rt, int L, int R, long long v) {
  int m = l + r >> 1;
  down(rt);
  if (l >= L && r <= R) {
    if (l == r) {
      query += min(mi[rt], (long long)v);
      if (v > mi[rt]) add(l, 1);
      mi[rt] = mi[rt] < v ? 1e18 : mi[rt] - v;
      return;
    } else if (mi[rt] >= v) {
      tag[rt] = v;
      mi[rt] -= v;
      query += (long long)(r - l + 1) * (long long)v;
      return;
    } else {
      modify(l, m, rt << 1, L, R, v);
      modify(m + 1, r, rt << 1 | 1, L, R, v);
    }
  } else {
    if (m >= L) modify(l, m, rt << 1, L, R, v);
    if (m < R) modify(m + 1, r, rt << 1 | 1, L, R, v);
  }
  mi[rt] = min(mi[rt << 1], mi[rt << 1 | 1]);
}
void f(int l, int r, int rt, int p) {
  if (l == r) {
    printf("mi[%d] = %lld\n", l, mi[rt]);
    return;
  }
  down(rt);
  int m = l + r >> 1;
  if (p <= m)
    f(l, m, rt << 1, p);
  else
    f(m + 1, r, rt << 1 | 1, p);
}
int main() {
  scanf("%lld%lld%lld", &n, &k, &m);
  for (int i = 1; i <= m; i++)
    scanf("%lld%lld%lld%lld", &p[i].l, &p[i].r, &p[i].p, &p[i].c);
  sort(p + 1, p + 1 + m, [](plan a, plan b) { return a.c < b.c; });
  long long ans = 0;
  build(1, n, 1);
  for (int i = 1; i <= m; i++) {
    query = 0;
    long long x = (long long)(getsum(p[i].r) - getsum(p[i].l - 1));
    int l = p[i].l, r = p[i].r;
    modify(1, n, 1, p[i].l, p[i].r, p[i].p);
    query -= x * (long long)p[i].p;
    ans += query * (long long)p[i].c;
  }
  printf("%lld\n", ans);
  return 0;
}
