#include<bits/stdc++.h>
using namespace std;
#define ll long long 
// memo  fixed  setprecision(20);
using vvll = vector<vector<ll>>;
ll mod =1e9+7;
/*"itob" int to "N"base */           template<typename TypeInt> string itob(const TypeInt v, int base){static const char table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";string ret;static numeric_limits<TypeInt> t;TypeInt n = v;if (t.is_signed) {if (v < 0) n *= -1;}while (n >= base) {ret += table[n%base];n /= base;}ret += table[n];if (t.is_signed) {if (v < 0 && base == 10) ret += '-';}reverse(ret.begin(), ret.end());return ret;}
/*"chmin" a = MAX*/                  template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
/*"chmax" a = MIN*/                  template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
/*"ctoi"  char to int*/              int ctoi(char c) {return c-'0';}
/*"gcd"   MAX Euclidean */           ll gcd(ll a,ll b){if(b==0)return a; return gcd(b,a%b);}
/*"lcm"   MIN*/                      ll lcm(ll a,ll b){ll g = gcd(a,b);return a/g*b;}
/*"primecheck"If prime,return true.*/bool primecheck(ll n){if(n < 2) return false;else{for(int i = 2; i * i <= n; i++){if(n % i == 0) return false;}return true;}}
string  reverserange(string s,ll a,ll b){reverse(s.begin()+a-1,s.begin()+b); return s;}
ll modpow(ll a,ll n, ll mod){ll res = 1;while(n>0){if (n%2==1){res = res * a % mod;}a = a * a % mod;n/=2;}return res;}
int main() {
    int n,m,q;
    cin >> n >> m >>q;
    vector<int>retu(10,1);
    vector<tuple<int,int,int,int>>que(q);
    for(int i=0;i<q;i++){
        int a,b,c,d;
        cin >>a >>b >>c>>d;
        a--;b--;
        que.at(i)=make_tuple(a,b,c,d);
    }
    ll ans = 0;
    for(int i=1;i<=m;i++){
        for(int j=i;j<=m;j++){
            for(int k=j;k<=m;k++){
                for(int l=k;l<=m;l++){
                    for(int d=l;d<=m;d++){
                        for(int ii=d;ii<=m;ii++){
                            for(int jj=ii;jj<=m;jj++){
                                for(int kk=jj;kk<=m;kk++){
                                    for(int zz=kk;zz<=m;zz++){
                                        for(int dd=zz;dd<=m;dd++){
                                            retu.at(0)=i;
                                            retu.at(1)=j;
                                            retu.at(2)=k;
                                            retu.at(3)=l;
                                            retu.at(4)=d;
                                            retu.at(5)=ii;
                                            retu.at(6)=jj;
                                            retu.at(7)=kk;
                                            retu.at(8)=zz;
                                            retu.at(9)=dd;
                                            ll che = 0;
                                            for(int w=0;w<q;w++){
                                                int a =get<0>(que.at(w));
                                                int b =get<1>(que.at(w));
                                                int c =get<2>(que.at(w));
                                                int d =get<3>(que.at(w));
                                                if(retu.at(b)-retu.at(a)==c){
                                                    che+=d;
                                                }
                                            }
                                            ans = max(ans,che);
                                     }
                                    }
                                }
                            }
                        }                     
                    }
                }
            }
        }
    }
    cout << ans << endl;
}
    



Measured memory_bytes: 3604