#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
void pre() {}
void solve() {}
long long k;
const long long inf = 1e18 + 7;
struct Node {
  Node *l = 0, *r = 0;
  long long lo, hi, madd = 0, cnt = 1, mn = 0, smn = -inf;
  long long val = 0;
  Node(long long lo, long long hi) : lo(lo), hi(hi) {}
  Node(vector<long long>& v, long long lo, long long hi) : lo(lo), hi(hi) {
    if (lo + 1 < hi) {
      long long mid = lo + (hi - lo) / 2;
      l = new Node(v, lo, mid);
      r = new Node(v, mid, hi);
      cnt = l->cnt + r->cnt;
    }
  }
  long long query(long long L, long long R) {
    if (R <= lo || hi <= L) return 0;
    if (L <= lo && hi <= R) return val;
    push();
    return l->query(L, R) + r->query(L, R);
  }
  void add(long long L, long long R, long long x) {
    if (R <= lo || hi <= L) return;
    if ((L <= lo && hi <= R && smn + x < k) || (lo + 1 >= hi)) {
      madd += x;
      if (mn + x <= k)
        val += 1ll * x * (hi - lo), mn += x;
      else
        val += 1ll * x * (hi - lo - cnt) + 1ll * (k - mn) * cnt, mn = k;
      if (smn != -inf) smn += x;
    } else {
      push(), l->add(L, R, x), r->add(L, R, x);
      mn = max(l->mn, r->mn);
      smn = max(l->smn, r->smn);
      cnt = 0;
      if (mn == r->mn)
        cnt += r->cnt;
      else
        smn = max(smn, r->mn);
      if (mn == l->mn)
        cnt += l->cnt;
      else
        smn = max(smn, l->mn);
      val = l->val + r->val;
    }
  }
  void push() {
    if (madd) l->add(lo, hi, madd), r->add(lo, hi, madd), madd = 0;
  }
};
vector<pair<pair<long long, long long>, pair<long long, long long>>> v;
vector<long long> xx;
signed main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  pre();
  long long n, m;
  cin >> n >> k >> m;
  for (long long i = 0; i < (m); ++i) {
    long long l, r, c, p;
    cin >> l >> r >> c >> p;
    v.push_back(make_pair(make_pair(p, c), make_pair(l, r)));
  }
  xx.resize(n, 0);
  Node T(xx, 0, n);
  sort(v.begin(), v.end());
  long long ans = 0;
  for (auto& i : v) {
    long long sum = T.query(i.second.first - 1, i.second.second);
    T.add(i.second.first - 1, i.second.second, i.first.second);
    long long sum2 = T.query(i.second.first - 1, i.second.second);
    ans += 1ll * (sum2 - sum) * i.first.first;
  }
  cout << ans;
  return 0;
}
