#include<stdio.h>

int a[50],b[50],c[50],d[50],q,n,m;

long score(int *ar){
    register int i;
    long res = 0;
    for(i = 0;i < q;i++)if(ar[b[i]-1] - ar[a[i]-1] == c[i])res += d[i];
    return  res;
}

void dfs(int *ar,int dep,long *max){
    register int i;
    if(dep == n){
        long tmp = score(ar);
        *max = *max < tmp?tmp:*max;
        return;
    }
    for(i = dep == 0?1:ar[dep-1];i <= m;i++){
        ar[dep] = i;
        dfs(ar,dep+1,max);
    }
}

int main(void){
    register int i;
    scanf("%d%d%d",&n,&m,&q);
    for(i = 0;i < q;i++)scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
    int ar[n];
    long cnt = 0;
    dfs(ar,0,&cnt);
    printf("%ld",cnt);
}