#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char c = getchar();
  while (c > '9' || c < '0') c = getchar();
  long long ans = 0;
  for (; c <= '9' && c >= '0'; c = getchar()) ans = ans * 10 + c - '0';
  return ans;
}
int n, m;
long long k, ans, tt, Pmax;
struct Plan {
  int t;
  long long c, p;
} p[400010];
inline bool tdn(Plan x, Plan y) { return x.t < y.t; }
struct Reply {
  int h = 0;
  long long ex = 0;
};
struct Node {
  long long cnt = 0, wgh = 0;
} node[4000010];
inline void push_up(int t) {
  node[t].cnt = node[t << 1].cnt + node[t << 1 | 1].cnt;
  node[t].wgh = node[t << 1].wgh + node[t << 1 | 1].wgh;
}
void update(int t, int l, int r, int x) {
  if (l == p[x].p && r == p[x].p) {
    node[t].cnt += p[x].c;
    node[t].wgh += p[x].c * p[x].p;
    return;
  }
  int mid = (l + r) >> 1;
  if (p[x].p <= mid)
    update(t << 1, l, mid, x);
  else
    update(t << 1 | 1, mid + 1, r, x);
  push_up(t);
}
Reply query(int t, int l, int r, long long num) {
  if (l == r) {
    Reply ans;
    if (num == node[t].cnt)
      ans.h = l;
    else
      ans.h = l - 1, ans.ex = l * num;
    return ans;
  }
  if (node[t].cnt == num) {
    Reply ans;
    ans.h = r;
    return ans;
  }
  int mid = (l + r) >> 1;
  if (node[t << 1].cnt >= num) return query(t << 1, l, mid, num);
  return query(t << 1 | 1, mid + 1, r, num - node[t << 1].cnt);
}
long long sum_query(int t, int l, int r, int L, int R) {
  if (L <= l && r <= R) return node[t].wgh;
  int mid = (l + r) >> 1;
  long long ans = 0;
  if (L <= mid) ans += sum_query(t << 1, l, mid, L, R);
  if (R > mid) ans += sum_query(t << 1 | 1, mid + 1, r, L, R);
  return ans;
}
int main() {
  n = read(), k = read(), m = read();
  for (int i = 1; i <= m; i++) {
    p[(i << 1) - 1].t = read(), p[i << 1].t = read() + 1;
    p[(i << 1) - 1].c = read(), p[i << 1].c = -p[(i << 1) - 1].c;
    p[(i << 1) - 1].p = read(), p[i << 1].p = p[(i << 1) - 1].p;
    if (p[i << 1].p > Pmax) Pmax = p[i << 1].p;
  }
  sort(p + 1, p + (m << 1 | 1), tdn);
  update(1, 1, Pmax, 1);
  for (int i = 2; i <= (m << 1); i++) {
    tt = p[i].t - p[i - 1].t;
    if (node[1].cnt <= k)
      ans += node[1].wgh * tt;
    else {
      Reply u = query(1, 1, Pmax, k);
      long long res = u.ex;
      if (u.h) res += sum_query(1, 1, Pmax, 1, u.h);
      ans += res * tt;
    }
    update(1, 1, Pmax, i);
  }
  printf("%lld", ans);
  return 0;
}
