#include<bits/stdc++.h>
using namespace std;
#define FOR(i,l,r) for(long long i=(l);i<(r);++i)
#define REP(i,n) FOR(i,0,n)
#define REPS(i,n) FOR(i,0,n+1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
const int MOD=1e9+7;
const int INF=1e18;

signed main(){
    int N,M,Q;cin>>N>>M>>Q;
    int A[N];
    A[0]=1;
    vector<pair<pair<int,int>,pair<int,int>>>vec(Q);
    int ans=0;
  	REP(i,Q){
        int a,b,c,d;cin>>a>>b>>c>>d;
        pair<int,int> z=mp(d,a);
        pair<int,int> x=mp(b,c);
        vec[i]=mp(z,x);
    }
    for(int i2=1;i2<=M;i2++){
        A[1]=i2;
      
        if(N==2){
            int preans=0;
            REP(i,Q){
                int a=vec[i].first.second-1;
                int b=vec[i].second.first-1;
                int c=vec[i].second.second;
                int d=vec[i].first.first;
                if(A[b]-A[a]==c)preans+=d;
            }
            ans=max(ans,preans);
        }
        else{
            for(int i3=i2;i3<=M;i3++){
            A[2]=i3;
            if(N==3){
            int preans=0;
            REP(i,Q){
                int a=vec[i].first.second-1;
                int b=vec[i].second.first-1;
                int c=vec[i].second.second;
                int d=vec[i].first.first;
                if(A[b]-A[a]==c)preans+=d;
            }
            ans=max(ans,preans);
            }
            else{
                for(int i4=i3;i4<=M;i4++){
            A[3]=i4;
            if(N==4){
            int preans=0;
            REP(i,Q){
                int a=vec[i].first.second-1;
                int b=vec[i].second.first-1;
                int c=vec[i].second.second;
                int d=vec[i].first.first;
                if(A[b]-A[a]==c)preans+=d;
            }
            ans=max(ans,preans);
            }
            else{
                    for(int i5=i4;i5<=M;i5++){
            A[4]=i5;
            if(N==5){
            int preans=0;
            REP(i,Q){
                int a=vec[i].first.second-1;
                int b=vec[i].second.first-1;
                int c=vec[i].second.second;
                int d=vec[i].first.first;
                if(A[b]-A[a]==c)preans+=d;
            }
            ans=max(ans,preans);
            }
            else{
                    for(int i6=i5;i6<=M;i6++){
            A[5]=i6;
            if(N==6){
            int preans=0;
            REP(i,Q){
                int a=vec[i].first.second-1;
                int b=vec[i].second.first-1;
                int c=vec[i].second.second;
                int d=vec[i].first.first;
                if(A[b]-A[a]==c)preans+=d;
            }
            ans=max(ans,preans);
            }
            else{
                    for(int i7=i6;i7<=M;i7++){
            A[6]=i7;
            if(N==7){
            int preans=0;
            REP(i,Q){
                int a=vec[i].first.second-1;
                int b=vec[i].second.first-1;
                int c=vec[i].second.second;
                int d=vec[i].first.first;
                if(A[b]-A[a]==c)preans+=d;
            }
            ans=max(ans,preans);
            }
            else{
                    for(int i8=i7;i8<=M;i8++){
            A[7]=i8;
            if(N==8){
            int preans=0;
            REP(i,Q){
                int a=vec[i].first.second-1;
                int b=vec[i].second.first-1;
                int c=vec[i].second.second;
                int d=vec[i].first.first;
                if(A[b]-A[a]==c)preans+=d;
            }
            ans=max(ans,preans);
            }
            else{
                    for(int i9=i8;i9<=M;i9++){
            A[8]=i9;
            if(N==9){
            int preans=0;
            REP(i,Q){
                int a=vec[i].first.second-1;
                int b=vec[i].second.first-1;
                int c=vec[i].second.second;
                int d=vec[i].first.first;
                if(A[b]-A[a]==c)preans+=d;
            }
            ans=max(ans,preans);
            }
            else{
                    for(int i10=i9;i10<=M;i10++){
            A[9]=i10;
            int preans=0;
            REP(i,Q){
                int a=vec[i].first.second-1;
                int b=vec[i].second.first-1;
                int c=vec[i].second.second;
                int d=vec[i].first.first;
                if(A[b]-A[a]==c)preans+=d;
            }
            ans=max(ans,preans);
            }
            }
            }
            }
            }
            }
            }
            }
            }
            }
                    }
            }
                }
            }
            }
        }
    }                
    cout<<ans<<endl;
}

Measured memory_bytes: 3648