#include <iostream>
using namespace std;
#define rep(i,n) for(int i=0; i<(int)(n); i++)

int m, n ,q;
int a[50], b[50], c[50], d[50];
int A[10];
int maxScore = -1;

void dfs(int dep, int x) {
    if (dep == n) {
        int score = 0;
        rep(i, q) if (A[b[i]] - A[a[i]] == c[i]) score += d[i];
        
        maxScore = max(maxScore, score);
        return;
    }
    
    for (int i = x; i <= n; i++) {
        A[dep] = i;
        dfs(dep+ 1, i);
    }
}

int main(void){
    cin >> m >> n >> q;
	rep(i, q) {
		cin >> a[i] >> b[i] >> c[i] >> d[i];
		a[i]--; b[i]--;
	}
	dfs(0, 1);
	cout << maxScore << endl;
	return 0;
}