#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAX = 1123456;
struct Bit {
  vector<ll> tree;
  Bit() {}
  Bit(int size) : tree(size + 1) {}
  ll getc(int end) {
    ll sum = 0ll;
    for (end++; end > 0; end -= end & -end) sum += tree[end];
    return sum;
  }
  void update(int pos, ll delta) {
    for (pos++; pos < tree.size(); pos += pos & -pos) tree[pos] += delta;
  }
};
Bit cnt(MAX);
Bit cost(MAX);
vector<pair<ll, ll>> sh[MAX];
vector<pair<ll, ll>> eh[MAX];
int n, k;
ll solveDay() {
  int lb = 0;
  int ub = MAX - 1;
  while (lb < ub) {
    int mid = (lb + ub) / 2;
    if (cnt.getc(mid) >= k) {
      ub = mid;
    } else {
      lb = mid + 1;
    }
  }
  ll total = cnt.getc(lb);
  if (total <= k) {
    return cost.getc(lb);
  }
  ll ans = 0;
  ll need = k;
  ll have = total;
  if (lb) {
    ans += cost.getc(lb - 1);
    ll bef = cnt.getc(lb - 1);
    need -= bef;
    have -= bef;
  }
  ans += need * lb;
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  int m;
  cin >> n >> k >> m;
  for (int i = 0; i < m; i++) {
    int li, ri;
    ll ci, pi;
    cin >> li >> ri >> ci >> pi;
    li--;
    ri--;
    sh[li].push_back({ci, pi});
    eh[ri].push_back({ci, pi});
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (auto e : sh[i]) {
      cnt.update(e.second, e.first);
      cost.update(e.second, e.first * e.second);
    }
    ans += solveDay();
    for (auto e : eh[i]) {
      cnt.update(e.second, -e.first);
      cost.update(e.second, -e.first * e.second);
    }
  }
  cout << ans << endl;
}
