#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define Rep(i,a,n)for(int i=(int)(a);i<(int)(n);i++)
#define rep(i,n)Rep(i,0,n)
typedef long long ll;
typedef long double ld;
#define Ma(x,y) ((x)>(y)?(x):(y))
#define mi(x,y) ((x)<(y)?(x):(y))

int main() {
	int n, m, q, a[4][52], ans=0;
	rep(i, 52)rep(j, 4)a[j][i]=0;
	scanf("%d %d %d", &n, &m, &q);
	rep(i,q)rep(j,4)scanf("%d", &a[j][i]);

	rep(i, 1024){
		int c = 0, point = 0, A[11] = { 0 }, all[11] = { 0 };
		if (pow(2, m) <= i)break;
		all[10] = i / 512;
		Rep(j, 2, 11)all[11 - j] = (i % (int)pow(2, 11 - j)) / (int)pow(2, 10 - j);
		Rep(j, 1, 11)if (all[j]){c++;A[c]=j;}if (c != n)continue;
		
		rep(j,q)if(A[a[1][j]] - A[a[0][j]]==a[2][j])point += a[3][j];
		ans = Ma(ans, point);
	}
	printf("%d", ans);
}