#include <stdio.h>

long int check(int Q, int A[], long int Group[][4])
{
  long int ans=0;
  for (int i = 0; i< Q; i++) {
    if (A[Group[i][1]] - A[Group[i][0]] == Group[i][2])
      ans += Group[i][3];
  }
  return ans;
}

int main(void)
{
  int N, M, Q;
  int ii, j, k, l, m, n, o, p, q, r;
  scanf("%d%d%d", &N, &M, &Q);
  int A[1+10];
  long int Group[Q][4], ans=0, tmp_ans=0;

  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < 4; j++)
      scanf("%ld", &Group[i][j]);
  }

  /*A[N] combination*/
  
  for (ii = 0;ii < M; ii++) {
    A[1] = ii+1;
    for (j = ii; j < M; j++) {
    A[2] = j+1;
    for (k = j; k < M; k++) {
    A[3] = k+1;
    for (l = k; l < M; l++) {
    A[4] = l+1;
    for (m = l; m < M; m++) {
    A[5] = m+1;
    for (n = l; n < M; n++) {
    A[6] = n+1;
    for (o = n; o < M; o++) {
    A[7] = o+1;
    for (p = o; p < M; p++) {
    A[8] = p+1;
    for (q = p; q < M; q++) {
    A[9] = q+1;
    for (r = q; r < M; r++) {
    A[10] = r+1;
    if ((tmp_ans = check(Q, A, Group)) > ans)
      ans = tmp_ans;
    }}}}}}}}}}
  




    printf("%ld\n", ans);

    return 0;
}
