#include <iostream>
#include <cstdio>
#define N 55
using namespace std;

int n, m, Q, mx, a[N], b[N], c[N], d[N], x[15];

void dfs(int p, int q) {
	int i, s=0;
	if(p==n+1) {
		for(i=1; i<=Q; i++) if(x[b[i]] - x[a[i]] == c[i]) s += d[i];
		mx = max(mx, s);
		return;
	}
	for(i=q; i<=m; i++) {
		x[p] = i;
		dfs(p+1, i);
	}
}

int main() {
	cin >> n >> m >> Q;
	for(int i=1; i<=Q; i++) cin >> a[i] >> b[i] >> c[i] >> d[i];
	dfs(1, 1);
	cout << mx;
	return 0;
}