/* 記録用 */
#include <stdio.h>
int main()
{
  int N, M, Q;
  int q[50][4];
  int A[10] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  int i;
  int index;
  int tempa, tempb, tempc, tempd;
  int total = 0;
  
  scanf("%d %d %d", &N, &M, &Q);
  for(i = 0; i < Q; ++i){
    scanf("%d %d %d %d", &q[i][0], &q[i][1], &q[i][2], &q[i][3]);
    index = i;
    while((index > 0) && (q[index][3] > q[index-1][3])){
      tempa = q[index][0];
      tempb = q[index][1];
      tempc = q[index][2];
      tempd = q[index][3];
      q[index][0] = q[index-1][0];
      q[index][1] = q[index-1][1];
      q[index][2] = q[index-1][2];
      q[index][3] = q[index-1][3];
      q[index-1][0] = tempa;
      q[index-1][1] = tempb;
      q[index-1][2] = tempc;
      q[index-1][3] = tempd;
      --index;
    }
  }
  for(i = 0; i < Q; ++i)
    printf("%d %d %d %d\n", q[i][0], q[i][1], q[i][2], q[i][3]);
  for(i = 0; i < Q; ++i){
    if(A[q[i][0] - 1] == 0)
      A[q[i][0] - 1] = q[i][0];
    if(A[q[i][1] - 1] == 0)
      A[q[i][1] - 1] = q[i][1] + 1;
    if((A[q[i][1] - 1] - A[q[i][0] - 1]) == q[i][2])
      total += q[i][3];
  }
  printf("%d\n", total);
  return 0;
}