#include <bits/stdc++.h>
using namespace std;


int N, M, Q;


vector<vector<int>> hozo;

void dfs(vector<int> A, int last) {
    int sz;
    sz = A.size();
    if (sz == N) {
        hozo.push_back(A);
        return;
    }

    for (int i = last; i <= M; i++) {

        vector<int> tmp;
        tmp = A;
        tmp.push_back(i);
        dfs(tmp,i);
    }
}


int main(){
 
    cin >> N >> M >> Q;
    vector<int> a(N), b(N), c(N), d(N);
    for(int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
    
    }
    int score;
    vector<int> empty;

    dfs(empty, 1);

    for (vector<int> A : hozo){
        long long score = 0;
        for (int i = 0; i < Q; i++){

            if (A[b[i]] - A[a[i]] == c[i]){
                score += d[i];
            }
        }
        int ans;

        ans = max(ans,score);
    }
    
    cout >> score >> endl;

}