#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
struct shop {
  int l, r, c, p;
  bool operator<(const shop& rhs) const {
    if (p == rhs.p) {
      if (l == rhs.l) {
        if (r == rhs.r) return c < rhs.c;
        return r < rhs.r;
      }
      return l < rhs.l;
    }
    return p < rhs.p;
  }
};
struct evnt {
  shop p;
  int type, startIdx;
  bool operator<(const evnt& rhs) const {
    if (startIdx == rhs.startIdx) return type < rhs.type;
    return startIdx < rhs.startIdx;
  }
};
struct BIT {
  vector<pair<long long, long long> > v;
  BIT(int s) { v.resize(s); }
  void clear() { v.clear(); }
  BIT() {}
  pair<long long, long long> get(int i) {
    i++;
    pair<long long, long long> r = {0, 0};
    while (i) {
      r.first += v[i - 1].first;
      r.second += v[i - 1].second;
      i -= i & -i;
    }
    return r;
  }
  void add(int i, pair<long long, long long> val) {
    i++;
    while (i <= (int)v.size()) {
      v[i - 1].first += val.first;
      v[i - 1].second += val.second;
      i += i & -i;
    }
  }
  int find(long long val) {
    int l = 0, r = v.size() - 1;
    while (l < r) {
      int mid = (r + l) / 2;
      if (get(mid).first >= val)
        r = mid;
      else
        l = mid + 1;
    }
    return l;
  }
};
set<shop> curShops;
vector<evnt> v;
int main() {
  int n, k, m;
  BIT bit(2000100);
  long long ans = 0;
  cin >> n >> k >> m;
  for (int i = 0; i < m; i++) {
    int l, r, c, p;
    cin >> l >> r >> c >> p;
    v.push_back({{l, r, c, p}, 0, l});
    v.push_back({{l, r, c, p}, 1, r + 1});
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (v[i].type == 0) {
      curShops.insert(v[i].p);
      bit.add(v[i].p.p, {v[i].p.c, v[i].p.c * 1LL * v[i].p.p});
    } else {
      curShops.erase(v[i].p);
      bit.add(v[i].p.p, {-v[i].p.c, -v[i].p.c * 1LL * v[i].p.p});
    }
    int days = 0;
    if (i < v.size() - 1) {
      days = v[i + 1].startIdx - v[i].startIdx;
    } else {
      days = n - v[i].startIdx + 1;
    }
    if (days == 0) continue;
    long long price = bit.find(k);
    pair<long long, long long> x = bit.get(price);
    if (x.first <= k)
      ans += x.second * days;
    else
      ans += (x.second - (price * (x.first - k))) * days;
  }
  cout << ans << endl;
  return 0;
}
