#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<queue>
#define _USE_MATH_DEFINES
#include<math.h>
#include<cmath>
#include<ctime>
#include<map>
#define R(i, n) for(int i = 0;i < n;i++)
#define S(a) scanf("%d", &a)
#define S2(a, b) scanf("%d%d", &a, &b)
#define S3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SL(a) scanf("%lld", &a)
#define SL2(a, b) scanf("%lld%lld", &a, &b)
#define SL3(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define SA(i, n, a) R(i, n) scanf("%d", a + i)
#define SAL(i, n, a) R(i, n) scanf("%lld", a + i)
#define SC(a) scanf("%c", &a);
#define P(a) printf("%d", a)
#define PL(a) printf("%lld", a)
using namespace std;
using ll = long long;
using PR = pair<int, int>;
const ll MOD = 1e+9 + 7;
const int INF = 1e+9;

int N, M, Q, QE[50][4];

int search(vector<int> & vec) {
	int sum = 0;
	R(i, Q) if (vec[QE[i][1] - 1] - vec[QE[i][0] - 1] == QE[i][2]) sum += QE[i][3];
	return sum;
}

int main() {
	S3(N, M, Q);
	R(i, Q) {
		int a = 0, b = 0, c = 0, d = 0;
		S3(a, b, c);
		S(d);
		QE[i][0] = a;
		QE[i][1] = b;
		QE[i][2] = c;
		QE[i][3] = d;
	}
	vector<int> num(N);
	int ans = 0;
	for (int su0 = 1; su0 <= M; su0++) {
		for (int su1 = su0; su1 <= M; su1++) {
			if (N == 2) {
				num[0] = su0;
				num[1] = su1;
				ans = max(ans, search(num));
			}
			else {
				for (int su2 = su1; su2 <= M; su2++) {
					if (N == 3) {
						num[0] = su0;
						num[1] = su1;
						num[2] = su2;
						ans = max(ans, search(num));
					}
					else {
						for (int su3 = su2; su3 <= M; su3++) {
							if (N == 4) {
								num[0] = su0;
								num[1] = su1;
								num[2] = su2;
								num[3] = su3;
								ans = max(ans, search(num));
							}
							else {
								for (int su4 = su3; su4 <= M; su4++) {
									if (N == 5) {
										num[0] = su0;
										num[1] = su1;
										num[2] = su2;
										num[3] = su3;
										num[4] = su4;
										ans = max(ans, search(num));
									}
									else {
										for (int su5 = su4; su5 <= M; su5++) {
											if (N == 6) {
												num[0] = su0;
												num[1] = su1;
												num[2] = su2;
												num[3] = su3;
												num[4] = su4;
												num[5] = su5;
												ans = max(ans, search(num));
											}
											else {
												for (int su6 = su5; su6 <= M; su6++) {
													if (N == 7) {
														num[0] = su0;
														num[1] = su1;
														num[2] = su2;
														num[3] = su3;
														num[4] = su4;
														num[5] = su5;
														num[6] = su6;
														ans = max(ans, search(num));
													}
													else {
														for (int su7 = su6; su7 <= M; su7++) {
															if (N == 8) {
																num[0] = su0;
																num[1] = su1;
																num[2] = su2;
																num[3] = su3;
																num[4] = su4;
																num[5] = su5;
																num[6] = su6;
																num[7] = su7;
																ans = max(ans, search(num));
															}
															else {
																for (int su8 = su7; su8 <= M; su8++) {
																	if (N == 9) {
																		num[0] = su0;
																		num[1] = su1;
																		num[2] = su2;
																		num[3] = su3;
																		num[4] = su4;
																		num[5] = su5;
																		num[6] = su6;
																		num[7] = su7;
																		num[8] = su8;
																		ans = max(ans, search(num));
																	}
																	else {
																		for (int su9 = su8; su9 <= M; su9++) {
																			if (N == 10) {
																				num[0] = su0;
																				num[1] = su1;
																				num[2] = su2;
																				num[3] = su3;
																				num[4] = su4;
																				num[5] = su5;
																				num[6] = su6;
																				num[7] = su7;
																				num[8] = su8;
																				num[9] = su9;
																				ans = max(ans, search(num));
																			}
																		}
																	}
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	P(ans);
}



Measured memory_bytes: 3760