#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rrep(i, n) for(int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL<<60;

int N,M,Q;
vector<int> a, b, c, d;
ll ans = 0;

void dfs(vector<int> A){
    if (A.size() == N){
             
        ll tmp = 0;
        rep(i,Q){
            if (A[b[i]] - A[a[i]] == c[i]){
                tmp += d[i];
            }
        }
        ans = max(ans, tmp);
        return;
    }

    A.push_back(A.back());
    while (A.back() <= M){
        dfs(A);
        A.back()++;
    }    
}

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

    for (int i = 1; i <= M; i++){
        dfs(vector<int>(1, i));
    }
    cout << ans << endl;
}