#include<stdio.h>
#include<math.h>
int main(){
   int n,m,q,i,j,k,score=0,max;
    scanf("%d%d%d", &n, &m, &q);
    int seq[n],a[50],b[50],c[50],d[50];
    for(i=0; i<q; i++){
        scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
    }
    for(i=0;i<n;i++){
       seq[i]=1;
    }
    max=score;
    while(1){
        score=0;
        for(i=0;i<q;i++){
            if((seq[b[i]-1]-seq[a[i]-1])==c[i]){
                score+=d[i];
            }
        }
        if(max<score){
            max=score;
        }

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

        seq[n-1]++;

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

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