#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <math.h>
#ifdef __cplusplus
#include <bits/stdc++.h>
#endif

uint32_t n, m, s;
uint32_t a[11];
uint32_t q[51][4];
uint64_t max;
void df(int d){
	if(d==0){
		uint64_t now=0;
		for(uint32_t i=s; i>0; i--){
			if(a[q[i][1]]-a[q[i][0]]==q[i][2]) now+=q[i][3];
		}
		if(now>max) max=now;
	}else{
		for(uint32_t i=a[d]; i>0; i--){
			a[d-1]=i;
			df(d-1);
		}
	}
}

int main(void){
	scanf("%"SCNu32"%"SCNu32"%"SCNu32, &n, &m, &s);
	for(uint32_t i=s; i>0; i--){
		scanf("%"SCNu32"%"SCNu32"%"SCNu32"%"SCNu32, &q[i][0], &q[i][1], &q[i][2], &q[i][3]);
		q[i][0]--;
		q[i][1]--;
	}
	a[n]=m;
	df(n);
	printf("%"PRIu64"\n", max);
}

