#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 1ll;
long long lim = 0, Ans = 0;
struct Plan {
  long long pos;
  long long c, p;
} a[N];
struct Tree {
  long long c, p;
  Tree() { c = p = 0; };
} t[N << 2ll];
struct Acht {
  long long a, b;
  Acht() { a = b = 0; };
};
bool ctrl(Plan u, Plan v) { return u.pos < v.pos; }
inline void pushup(long long u) {
  t[u].c = t[u << 1ll].c + t[u << 1ll | 1ll].c;
  t[u].p = t[u << 1ll].p + t[u << 1ll | 1ll].p;
}
void update(long long u, long long l, long long r, long long pos) {
  if (l == r && l == a[pos].p) {
    t[u].c += a[pos].c;
    t[u].p += a[pos].c * a[pos].p;
    return;
  }
  long long mid = (l + r) >> 1ll;
  if (a[pos].p <= mid)
    update(u << 1ll, l, mid, pos);
  else
    update(u << 1ll | 1ll, mid + 1ll, r, pos);
  pushup(u);
}
Acht query(long long u, long long l, long long r, long long rnk) {
  if (l == r) {
    Acht res;
    if (rnk == t[u].c)
      res.a = l;
    else
      res.a = l - 1ll, res.b = l * rnk;
    return res;
  }
  if (t[u].c == rnk) {
    Acht res;
    res.a = r;
    return res;
  }
  long long mid = (l + r) >> 1ll;
  if (t[u << 1ll].c >= rnk) return query(u << 1ll, l, mid, rnk);
  return query(u << 1ll | 1ll, mid + 1ll, r, rnk - t[u << 1ll].c);
}
long long query(long long u, long long l, long long r, long long a,
                long long b) {
  if (a <= l && r <= b) return t[u].p;
  long long mid = (l + r) >> 1ll;
  long long res = 0;
  if (a <= mid) res += query(u << 1ll, l, mid, a, b);
  if (b > mid) res += query(u << 1ll | 1ll, mid + 1ll, r, a, b);
  return res;
}
signed main() {
  long long n, k, m;
  scanf("%lld%lld%lld", &n, &k, &m);
  for (long long i = 1ll; i <= m; ++i) {
    long long y = i << 1ll, x = y - 1ll;
    scanf("%lld%lld%lld%lld", &a[x].pos, &a[y].pos, &a[x].c, &a[x].p);
    a[y].pos++, a[y].c = -a[x].c, a[y].p = a[x].p;
    lim = max(lim, a[x].p);
  }
  sort(a + 1ll, a + (m << 1ll | 1ll), ctrl);
  update(1ll, 1ll, lim, 1ll);
  for (long long i = 2; i <= (m << 1ll); ++i) {
    long long itv = a[i].pos - a[i - 1ll].pos;
    if (t[1ll].c <= k)
      Ans += t[1ll].p * itv;
    else {
      Acht tmp = query(1ll, 1ll, lim, k);
      long long res = tmp.b;
      if (tmp.a) res += query(1ll, 1ll, lim, 1ll, tmp.a);
      Ans += res * itv;
    }
    update(1ll, 1ll, lim, i);
  }
  printf("%lld", Ans);
  return 0;
}
