#include <bits/stdc++.h>
using namespace std;
long long k;
struct Set2 {
  multiset<pair<long long, long long> > s1, s2;
  long long total1 = 0;
  long long cnt1 = 0;
  void push12() {
    total1 -= (*s1.rbegin()).first * (*s1.rbegin()).second;
    cnt1 -= (*s1.rbegin()).second;
    s2.insert(*s1.rbegin());
    auto it = s1.end();
    it--;
    s1.erase(it);
  }
  void push21() {
    total1 += (*s2.begin()).first * (*s2.begin()).second;
    cnt1 += (*s2.begin()).second;
    s1.insert(*s2.begin());
    auto it = s2.begin();
    s2.erase(it);
  }
  void cutoff() {
    while (s1.size() && cnt1 - (*s1.rbegin()).second >= k) {
      push12();
    }
    while (s2.size() && cnt1 < k) {
      push21();
    }
  }
  void balance() {
    while (s1.size() && s2.size() &&
           (*s1.rbegin()).first > (*s2.begin()).first) {
      push12();
      push21();
    }
  }
  void insert(pair<long long, long long> a) {
    s2.insert(a);
    for (int i = 0; i < 7; i++) cutoff(), balance();
  }
  void erase(pair<long long, long long> a) {
    if (s1.find(a) != s1.end()) {
      total1 -= a.second * a.first;
      cnt1 -= a.second;
      s1.erase(s1.find(a));
    } else if (s2.find(a) != s2.end()) {
      s2.erase(s2.find(a));
    }
    for (int i = 0; i < 7; i++) cutoff(), balance();
  }
  long long get() {
    if (cnt1 <= k) return total1;
    if (s1.size() == 0) return 0;
    long long D = cnt1 - k;
    return total1 - D * (*s1.rbegin()).first;
  }
};
void solve() {
  long long n, m;
  cin >> n >> k >> m;
  vector<vector<pair<long long, long long> > > on(n);
  vector<vector<pair<long long, long long> > > off(n);
  for (int i = 0; i < m; i++) {
    int l, r, c, p;
    cin >> l >> r >> c >> p;
    l--, r--;
    on[l].push_back({p, c});
    off[r].push_back({p, c});
  }
  long long ans = 0;
  Set2 s;
  for (int i = 0; i < n; i++) {
    for (auto e : on[i]) s.insert(e);
    ans += s.get();
    for (auto e : off[i]) s.erase(e);
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
}
