#include <bits/stdc++.h>
using namespace std;
 
int n, m, q, a[50], b[50], c[50], d[50], ans = 0, arr[10];
 
void dfs(int l, int p) {
	if (l == n) {
		int v = 0;
		for (int i = 0; i < q; i++)
			if (arr[b[i]] - arr[a[i]] == c[i])
				v += d[i];
		ans = max(ans, v);
	} else {
		for (int i = p; i <= m; i++) {
			arr[l] = i;
			dfs(l + 1, i);
		}
	}
}
 
int main() {
	cin >> n >> m >> q;
	for (int i = 0; i < q; i++) {
		cin >> a[i] >> b[i] >> c[i] >> d[i];
		a[i]--, b[i]--;
	}
	dfs(0, 1);
	cout << ans << endl;
}