#include <stdio.h>

int main(void){

  int n,m,q;
  scanf("%d %d %d", &n, &m, &q);
  int a[q],b[q],c[q];
  long d[q];
  for (long i = 0; i < q; i++) {
    scanf("%d %d %d %ld", &a[i], &b[i], &c[i], &d[i]);
  }
  long max = 0;
  long score;
  int sequence[n];
  for (int i = 0; i < n; i++) {
    sequence[i] = 1;
  }
  while (1) {
    score = 0;
    for (int i = 0; i < q; i++) {
      if (sequence[b[i]-1]-sequence[a[i]-1] == c[i]) {
        score += d[i];
      }
    }
    if (score > max) {
      max = score;
    }
    if (sequence[1] == m) {
      break;
    }
    for (int i = n-1; i > 0; i--) {
      if (sequence[i] < m) {
        sequence[i]++;
        for (int j = i+1; j < n; j++) {
          sequence[j] = sequence[i];
        }
        break;
      }
    }
  }
  printf("%ld\n", max);

  return 0;
}