#include <bits/stdc++.h>
using namespace std;
long long int cons;
inline long long int check(long long int a) {
  if (a >= cons) a %= cons;
  return a;
}
inline long long int check2(long long int a) {
  a %= cons;
  if (a < 0) a += cons;
  return a;
}
inline long long int fast_check(long long int a) {
  if (a >= cons) a -= cons;
  return a;
}
long long int GCD(long long int a, long long int b) {
  while (a && b) {
    a = a % b;
    if (a != 0) b = b % a;
  }
  return a + b;
}
long long int exp(long long int a, long long int n) {
  long long int ans = 1;
  a = check(a);
  while (n) {
    if (n & 1) ans = check(ans * a);
    a = check(a * a);
    n = (n >> 1);
  }
  return ans;
}
void print_pair(pair<int, int> p) {
  cout << p.first << " " << p.second << "\n";
}
long long int nc2(long long int a) { return (a * (a - 1)) / 2; }
const int N = 2e5 + 10;
const int M = 1e6 + 10;
long long int cost[N];
struct C1 {
  bool operator()(int a, int b) {
    if (cost[a] != cost[b]) return cost[a] > cost[b];
    return a > b;
  }
};
struct C2 {
  bool operator()(int a, int b) {
    if (cost[a] != cost[b]) return cost[a] < cost[b];
    return a < b;
  }
};
set<int, C1> active;
set<int, C2> available;
vector<int> beg[M], fin[M];
int in_active[N], in_avail[N];
int with_me = 0;
long long int ans = 0;
long long int cur_cost = 0;
int n, k, m;
void remov(int i) {
  if (in_active[i]) {
    with_me -= in_active[i];
    active.erase(i);
    cur_cost -= in_active[i] * cost[i];
    in_active[i] = 0;
  }
  if (in_avail[i]) {
    available.erase(i);
    in_avail[i] = 0;
  }
}
void fill() {
  while (with_me < k && !available.empty()) {
    auto itr = *(available.begin());
    int can_give = in_avail[itr];
    int actual_give = min(k - with_me, can_give);
    cur_cost += cost[itr] * actual_give;
    with_me += actual_give;
    in_avail[itr] -= actual_give;
    if (!in_avail[itr]) available.erase(itr);
    if (!in_active[itr]) active.insert(itr);
    in_active[itr] += actual_give;
  }
}
void improve() {
  if (with_me < k) return;
  while (!active.empty() && !available.empty()) {
    auto itr_ac = *(active.begin());
    auto itr_av = *(available.begin());
    if (cost[itr_ac] <= cost[itr_av]) break;
    int change = min(in_active[itr_ac], in_avail[itr_av]);
    in_active[itr_ac] -= change;
    cur_cost -= change * cost[itr_ac];
    if (!in_active[itr_ac]) active.erase(itr_ac);
    if (!in_avail[itr_ac]) available.insert(itr_ac);
    in_avail[itr_ac] += change;
    cur_cost += change * cost[itr_av];
    in_avail[itr_av] -= change;
    if (!in_avail[itr_av]) available.erase(itr_av);
    if (!in_active[itr_av]) active.insert(itr_av);
    in_active[itr_av] += change;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cons = 1e9 + 7;
  srand(time(NULL));
  cin >> n >> k >> m;
  for (int i = 1; i <= m; i++) {
    int l, r, c, p;
    cin >> l >> r >> c >> p;
    in_avail[i] = c;
    cost[i] = p;
    beg[l].push_back(i);
    fin[r + 1].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    for (auto itr : fin[i]) remov(itr);
    for (auto itr : beg[i]) available.insert(itr);
    fill();
    improve();
    ans += cur_cost;
  }
  cout << ans;
  return 0;
}
