#include <stdio.h>
 
int yee(int nokori,int keta, int n,int* suretsu[10],int* qa[0],int* qb[0],int* qc[0],int* qd[0],int q){
    
    for(int i =0;i<nokori;i++)
    {suretsu[keta]=i+suretsu[keta-1];
    keta++;
    if(keta<n){
    return yee(nokori,keta,n,&suretsu[0],&qa[0],&qb[0],&qc[0],&qd[0],q);
    }
    else{
    int max=0;
    for(int i = 0;i<q;i++){
        int bb=qb[i];
        int aa=qa[i];
        int cc=qc[i];
        int dd=qd[i];
    if(suretsu[bb]-suretsu[aa]==cc){
    max+=dd;
    return max;
    }
    }
    }
}

int main()
{
    int n,m,q;
    scanf("%d %d %d", &n,&m,&q);
    int a,b,c,d;
    int max;
    
for(int i = 0;i<q;i++){
    scanf("%d %d %d %d",&a,&b,&c,&d);
    int qa[i]=a;
    int qb[i]=b;
    int qc[i]=c;
    int qd[i]=d;
}
int max;
int suretsu[11];
int nokori = m-1;
int keta = 0;
suretsu[0]=1;
max=yee(nokori,keta,n,&suretsu[0],&qa[0],&qb[0],&qc[0],&qd[0],q);

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