#include <stdio.h>
#define R(i,a,n)for(int i=(int)(a);i<=(int)(n);i++)
#define Ma(x,y) ((x)>(y)?(x):(y))

int main() {
	int n, m, q, a[4][52], A[11], point; long long ans = 0;
	R(i, 1, 50)R(j, 0, 3)a[j][i] = 0;
	scanf("%d %d %d", &n, &m, &q);
	R(i, 1, q)R(j, 0, 3)scanf("%d", &a[j][i]);
	R(Z, 1, m) {
		A[1] = Z; R(X, Z, m) {
			A[2] = X; R(C, X, m) {
				A[3] = C; R(V, C, m) {
					A[4] = V; R(B, V, m) {
						A[5] = B; R(N, B, m) {
							A[6] = N; R(M, B, m) {
								A[7] = M; R(Q, M, m) {
									A[8] = Q; R(W, Q, m) {
										A[9] = W; R(E, W, m) {
											A[10] = E;
											point = 0;
											R(j, 1, q)if (A[a[1][j]] - A[a[0][j]] == a[2][j])point += a[3][j];
											ans = Ma(ans, point);
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	printf("%lld", ans);
}