#include<stdio.h>
#include<math.h>
int main()
{
    int n, m, q, i, c, k, score=0, max;
    scanf("%d%d%d", &n, &m, &q);

    int seq[n], ai[50], bi[50], ci[50], di[50];

    for(i=0; i<q; i++){
        scanf("%d%d%d%d",  &ai[i], &bi[i], &ci[i], &di[i]);
    }

    for(i=0; i<n; i++){
        seq[i]=1;
    }
    max=score;
    while(1){

        score=0;
        for(i=0; i<q; i++){
            if((seq[bi[i]-1]-seq[ai[i]-1])==ci[i]){
                score+=di[i];
            }
        }
        if(max<score){
            max=score;
        }

        if(seq[0]==m) break;

        seq[n-1]++;

        if(seq[n-1]>m){
            for(c=n-1; c>=0; c--){
                if(seq[c]>m){
                    seq[c-1]++;
                    for(k=c; k<n; k++){
                        seq[k]=seq[c-1];
                    }
                }
                else break;
            }
        }
    }

    printf("%d\n", max);
    return 0;
}