#include <stdio.h>
#include <stdlib.h>
#define MAX(x,y) ((x)<(y)?(y):(x))
 
typedef long long ll;
 
typedef struct Hoge{
    int a;
    int b;
    int c;
    ll d;
}hoge;
 
void search(int i,int N,int M,int Q,int *A,hoge *AL,ll *max){
    if(i==M+1){
        int tmp = 0;
        for(int q = 0;q<Q;q++){
            if(A[AL[q].b]- A[AL[q].a] == AL[q].c) tmp+=AL[q].d;
        }
        *max = MAX(*max,tmp);
    }else if(i==1){
        for(int j = 1;j<=M;j++){
            A[i]=j;
            search(i+1,N,M,Q,A,AL,max);
        }
    }else{
        for(int j = A[i-1];j<=M;j++){
            A[i]=j;
            search(i+1,N,M,Q,A,AL,max);
        }
    }
}
 
int main(){
    int N,M,Q;
    scanf("%d %d %d",&N,&M,&Q);
    int A[N];
    hoge AL[Q];
    for(int i = 0;i<N;i++) A[i]=0;
    ll ans=0;
    for(int i = 0;i<Q;i++)scanf("%d %d %d %lld",&AL[i].a,&AL[i].b,&AL[i].c,&AL[i].d);
    search(1,N,M,Q,A,AL,&ans);
    printf("%lld\n",ans);
    
    return 0;
}