#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define rep(i,k,n) for(int (i)=(k);(i)<(n);(i)++)
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
#define abs(a,b) (a)< 0 ?(a):-(a)
#define llong long long

int next(int*,int,int);

int main(){
	int N,M,Q; scanf("%d %d %d",&N,&M,&Q);
	int a[Q],b[Q],c[Q],d[Q];
	rep(i,0,Q){
		scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]);
		a[i]--; b[i]--;
	}
	int A[N];
	rep(i,0,N) A[i]=0;
	int max = 0;
	do{
		int point = 0;
		rep(i,0,Q){
			if( A[b[i]] - A[a[i]] == c[i] ){
				point += d[i];
			}
		}
		max = max(max,point);
	}while( next(A,N,M) );
	printf("%d",max);
	return 0;
}

int next(int *A,int N,int M){
	rep(i,1,N+1){
		if( A[N-i] != M ){
			A[N-i]++;
			rep(j,N-i+1,N) A[j]=A[j-1];
			return 1;
		}
	}
	return 0;
}
