#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for(ll i=0; i<(ll)n; i++)
#define REPR(i, n) for(ll i=(ll)n-1; i>=0; i--)
#define FOR(i, m, n) for(ll i=(ll)m; i<(ll)n; i++)
#define FORR(i, m, n) for(ll i=(ll)m-1; i>=(ll)n; i--)
#define ALL(x) (x).begin(), (x).end()
#define ASC(v) sort(ALL(v));
#define DESC(v) sort(ALL(v), greater<int>());
#define UNIQ(a) sort(ALL(a));(a).resize(unique(ALL(a)) - a.begin());
#define PRINTD(x, d) cout << fixed << setprecision(d) << x << "\n";
#define DEBUG(x) cout<<#x<<": "<<x<<"\n"
const double PI = acos(-1);
ll lcm(ll a, ll b) { return a / __gcd(a, b) * b; }
bool isprime(int x){ int i; if(x<2)return 0; else if(x==2) return 1; if(x%2==0) return 0; for(i=3; i*i<=x; i+=2) if(x%i==0) return 0; return 1; }
int digsum(int n) { int r=0; while(n>0) { r+=n%10; n/=10; } return r; }
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; }
struct nya{ nya(){ cin.tie(0); ios::sync_with_stdio(false); } }nyan;


int main(){
    ll N, M, Q;
    cin >> N >> M >> Q;
    vector<map<char, ll>> q(Q);
    REP(i, Q) {
        ll t;
        cin >> t;
        q[i]['a'] = t;
        cin >> t;
        q[i]['b'] = t;
        cin >> t;
        q[i]['c'] = t;
        cin >> t;
        q[i]['d'] = t;
    }

    ll res = 0;
    FOR(i1, 1, M+1) {
        FOR(i2, i1, M+1) {
            FOR(i3, i2, M+1) {
                FOR(i4, i3, M+1) {
                    FOR(i5, i4, M+1) {
                        FOR(i6, i5, M+1) {
                            FOR(i7, i6, M+1) {
                                FOR(i8, i7, M+1) {
                                    FOR(i9, i8, M+1) {
                                        FOR(i10, i9, M+1) {
                                            vector<ll> A(10);
                                            A[0] = i1;
                                            A[1] = i2;
                                            A[2] = i3;
                                            A[3] = i4;
                                            A[4] = i5;
                                            A[5] = i6;
                                            A[6] = i7;
                                            A[7] = i8;
                                            A[8] = i9;
                                            A[9] = i10;
                                            ll sum = 0;
                                            REP(i, Q) {
                                                ll a = q[i]['a']-1;
                                                ll b = q[i]['b']-1;
                                                ll c = q[i]['c'];
                                                ll d = q[i]['d'];
                                                if (A[b]-A[a] == c) sum += d;
                                            }
                                            chmax(res, sum);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    cout << res << "\n";
}


Measured memory_bytes: 3676