#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1e5+5;
ll ans,n,m,q;
class obj{
public:
    ll a,b,c,d;
}queries[55];
void func(ll idx,ll mx,vector<ll> v){
    if(idx==n){
        ll sum = 0;
        for(ll i=0;i<q;i++){
            if(v[queries[i].b-1]-v[queries[i].a-1] == queries[i].c)
                sum+=queries[i].d;
        }
        ans=max(ans , sum);
        return;
    }
    for(ll i=mx;i<=m;i++){
        v.push_back(i);
        func(idx+1,i,v);
        v.pop_back();
    }
}
int main(){
    ios_base:: sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin>>n>>m>>q;
    for(ll i=0;i<q;i++)cin>>queries[i].a>>queries[i].b>>queries[i].c>>queries[i].d;
    vector<ll> v;
    func(0,1,v);
    cout<<ans;
}
