#include <math.h>
#include <stdio.h>
int main() {
	int n, m, q, p[10], A[10], a[50], b[50], c[50], d[50];
	int sum, max = 0;
	scanf("%d%d%d", &n, &m, &q);
	for (int i = 0; i < q; i++) {
		scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
	}
	for (int fi = 1; fi <= m; fi++) {
		int l = pow(m, n - 1);
		A[0] = fi;
		for (int bit = 0; bit < l; bit++) {
			int num = bit;
			for (int i = 1; i < n; i++) {
				p[n - i - 1] = (num % m);
				num /= m;
			}
			for (int i = 1; i < n; i++) {
				A[i] = A[i - 1] + p[i - 1];
			}
			if (A[n - 1] > m) {
				bit = bit - bit % (int)pow(m, n - 2) + pow(m, n - 2);
			} else {
				/*
				for (int i = 0; i < n; i++) {
					printf("%d ", A[i]);
				}
				printf("\n");
				*/
				sum = 0;
				for (int i = 0; i < q; i++) {
					if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
						sum += d[i];
					}
				}
				if (sum > max) {
					max = sum;
				}
			}
		}
	}
	printf("%d\n", max);
	return 0;
}