#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdlib>
#include <map>
#include <iomanip>
#include <set>
#include <stdio.h>
#include <ctype.h>
#include <random>
#include <string.h>
using namespace std;
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define int long long
#define P pair<int,int>
#define all(vec) vec.begin(),vec.end()

int gcd(int x, int y) {
	if (y == 0)return x;
	return gcd(y, x%y);
}
int lcm(int x, int y) {
	return x / gcd(x, y)*y;
}
int kai(int x, int y) {
	int res = 1;
	for (int i = x - y + 1; i <= x; i++) {
		res *= i; res %= mod;
	}
	return res;
}
int mod_pow(int x, int y, int m) {
	int res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % m;
		}
		x = x * x % m;
		y >>= 1;
	}
	return res;
}

int comb(int x, int y) {
	if (y > x)return 0;
	return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}

int n, m, q;
int a[55], b[55], c[55], d[55];
int x[15];
int ans;

signed main() {
	cin >> n >> m >> q;
	for (int i = 0; i < q; i++)cin >> a[i] >> b[i] >> c[i] >> d[i];
	for (int i = 1; i <= m; i++) {
		for (int j = i; j <= m; j++) {
			if (n == 2) {
				x[1] = i;
				x[2] = j;
				int cnt = 0;
				for (int k = 0; k < q; k++)if (x[b[k]] - x[a[k]] == c[k])cnt += d[k];
				ans = max(ans, cnt);
			}
			else for (int k = j; k <= m; k++) {
				if (n == 3) {
					x[1] = i;
					x[2] = j;
					x[3] = k;
					int cnt = 0;
					for (int l = 0; l < q; l++)if (x[b[l]] - x[a[l]] == c[l])cnt += d[l];
					ans = max(ans, cnt);
				}
				else {
					for (int l = k; l <= m; l++) {
						if (n == 4) {
							x[1] = i;
							x[2] = j;
							x[3] = k;
							x[4] = l;
							int cnt = 0;
							for (int o = 0; o < q; o++)if (x[b[o]] - x[a[o]] == c[o])cnt += d[o];
							ans = max(ans, cnt);
						}
						else for (int o = l; o <= m; o++) {
							if (n == 5) {
								x[1] = i;
								x[2] = j;
								x[3] = k;
								x[4] = l;
								x[5] = o;
								int cnt = 0;
								for (int p = 0; p < q; p++)if (x[b[p]] - x[a[p]] == c[p])cnt += d[p];
								ans = max(ans, cnt);
							}
							else for (int p = o; p <= m; p++) {
								if (n == 6) {
									x[1] = i;
									x[2] = j;
									x[3] = k;
									x[4] = l;
									x[5] = o;
									x[6] = p;
									int cnt = 0;
									for (int r = 0; r < q; r++)if (x[b[r]] - x[a[r]] == c[r])cnt += d[r];
									ans = max(ans, cnt);
								}
								else for (int r = p; r <= m; r++) {
									if (n == 7) {
										x[1] = i;
										x[2] = j;
										x[3] = k;
										x[4] = l;
										x[5] = o;
										x[6] = p;
										x[7] = r;
										int cnt = 0;
										for (int s = 0; s < q; s++)if (x[b[s]] - x[a[s]] == c[s])cnt += d[s];
										ans = max(ans, cnt);
									}
									else for (int s = r; s <= m; s++) {
										if (n == 8) {
											x[1] = i;
											x[2] = j;
											x[3] = k;
											x[4] = l;
											x[5] = o;
											x[6] = p;
											x[7] = r;
											x[8] = s;
											int cnt = 0;
											for (int t = 0; t < q; t++)if (x[b[t]] - x[a[t]] == c[t])cnt += d[t];
											ans = max(ans, cnt);
										}
										else for (int t = s; t <= m; t++) {
											if (n == 9) {
												x[1] = i;
												x[2] = j;
												x[3] = k;
												x[4] = l;
												x[5] = o;
												x[6] = p;
												x[7] = r;
												x[8] = s;
												x[9] = t;
												int cnt = 0;
												for (int u = 0; u < q; u++)if (x[b[u]] - x[a[u]] == c[u])cnt += d[u];
												ans = max(ans, cnt);
											}
											else for (int u = t; u <= m; u++) {
												x[1] = i;
												x[2] = j;
												x[3] = k;
												x[4] = l;
												x[5] = o;
												x[6] = p;
												x[7] = r;
												x[8] = s;
												x[9] = t;
												x[10] = u;
												int cnt = 0;
												for (int v = 0; v < q; v++)if (x[b[v]] - x[a[v]] == c[v])cnt += d[v];
												ans = max(ans, cnt);
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 3636