#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>

int main() {
	int n, m, Q;
	int val[50][4] = { 0 };
	int a[10] = { 0 };

	scanf("%d%d%d", &n, &m, &Q);
	for (int i = 0; i < Q; i++)
		scanf("%d%d%d%d", &val[Q][0], &val[Q][1], &val[Q][2], &val[Q][3]);

	int max = 0;
	for (int i = 2; i <= 10; i++)
		for (int j = 2; j <= 10; j++)
			for (int k = 2; k <= 10; k++)
				for (int l = 2; l <= 10; l++)
					for (int m = 2; m <= 10; m++)
						for (int n = 2; n <= 10; n++)
							for (int o = 2; o <= 10; o++)
								for (int p = 2; p <= 10; p++)
									for (int q = 2; q <= 10; q++)
										for (int r = 2; r <= 10; r++) {
											a[0] = i;
											a[1] = j;
											a[2] = k;
											a[3] = l;
											a[4] = m;
											a[5] = n;
											a[6] = o;
											a[7] = p;
											a[8] = q;
											a[9] = r;

											int ans = 0;
											for (int s = 0; s < Q; s++) {
												if (a[val[Q][1]] - a[val[Q][0]] == val[Q][2]) ans += val[Q][3];
											}
											if (ans > max) max = ans;
										}
	printf("%d", max);
	return 0;
}