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

int ans,n,m,q;
vector<int> a(55),b(55),c(55),d(55);

void dfs(vector<int> A){
    if(A.size()==n){
        int now=0;
        for(int i=0;i<q;i++){
            if(A[b[i]-1]-A[a[i]-1]==c[i]){
                now+=d[i];
            }
        }
        ans=max(ans,now);
        return;
    }
    if(A.size()==0){
        A.push_back(1);
    }
    else{
        A.push_back(A.back());
    }
    while(A.back()<=m){
        dfs(A);
        A.back()++;
    }
    
}

int main(){
    cin>>n>>m>>q;
    for(int i=0;i<q;i++){
        cin>>a[i]>>b[i]>>c[i]>>d[i];
    }

    ans=0;
    dfs(vector<int>(0));

    cout<<ans<<endl;    
}