#include <bits/stdc++.h>
#ifdef __LOCAL
    #define DBG(X) cout << #X << " = " << (X) << endl;
    #define SAY(X) cout << (X) << endl;
    #include <filesystem>
    namespace fs = std::filesystem;
#else
    #define DBG(X)
    #define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[]={1, 0, -1, 0, 1,-1, 1,-1};
int dy[]={0, 1, 0, -1, 1, 1,-1,-1};
const int INT_INF = (int)(2e9);
const ll  LL_INF = (ll)(2e18);
static mt19937 _g(time(nullptr));
 
inline ll randint(ll a, ll b) { ll w = (_g() << 31LL) ^ _g(); return a + w % (b - a + 1); }
inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };
template<typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; }
template<typename T, typename S> inline ostream& operator<<(ostream& os, const map<T, S> p) { for (auto el : p) cout << "[" << el.first << ";" << el.second << "]"; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto& elem : ret) cin >> elem; return ret; }

int N,M,Q;
struct query{
    int a,b,c;
    ll d;
    query(int a,int b,int c,ll d):a(a),b(b),c(c),d(d){}
};
vector<query> queries;

void input(){
    fast_io();
    #ifdef __LOCAL
        fs::path __p = __FILE__;
        fs::path __input, __output;
        __input = __output = __p.parent_path();
        __input += string("/input/") + string(__p.stem()) + string(".txt");
        __output += string("/output/") + string(__p.stem()) + string(".txt");
        freopen(__input.c_str(), "r", stdin);
        freopen(__output.c_str(), "w", stdout);
    #endif
    cin >> N >> M >> Q;
    for (int i = 0; i < Q; i++)
    {
        int a,b,c;
        ll d;
        cin >> a >> b >> c >> d;
        a--,b--;
        queries.emplace_back(a,b,c,d);
    }
    sort(queries.begin(),queries.end(),[](query p,query q){return p.a < q.a;});    
}

map<pair<int,vector<int> >,ll> memo;
ll dfs(int i,vector<int> A){
    if(i>=Q) return 0;
    if(memo[make_pair(i,A)]!=0) return memo[make_pair(i,A)];
    ll ret = 0;
    vector<int> tmp = A;
    // i番目をとる場合。
    if(tmp[queries[i].b]==-1){
        // if(A[queries[i].a]==-1) A[queries[i].a]=max(0,*max_element(A.begin(),A.begin()+queries[i].a));
        if(tmp[queries[i].a]==-1) tmp[queries[i].a]=max(0,*max_element(tmp.begin(),tmp.begin()+queries[i].a));
        tmp[queries[i].b] = tmp[queries[i].a]+queries[i].c;
        if(tmp[queries[i].b]>=M) ret = -1e9;
        else ret = queries[i].d+dfs(i+1,tmp);
    }
    else{
        // if(A[queries[i].a]==-1) A[queries[i].a]=max(0,*max_element(A.begin(),A.begin()+queries[i].a));
        if(tmp[queries[i].a]==-1) {
            tmp[queries[i].a]=tmp[queries[i].b]-queries[i].c;
            if(tmp[queries[i].a]>=*max_element(tmp.begin(),tmp.begin()+queries[i].a)) ret = queries[i].d+dfs(i+1,tmp);
            else ret = -1e9;
            }
        else if((tmp[queries[i].b]-tmp[queries[i].a])==queries[i].c) ret = queries[i].d+dfs(i+1,tmp);
        else ret = -1e9;
    }
    // if(tmp[queries[i].b]>=M) ret = -1e9;
    // i番目をスルーする場合。
    ret = max(ret,dfs(i+1,A));
    return memo[make_pair(i,A)] = ret;
}

ll calc(vector<int> A){
    ll ret=0;
    for (int i = 0; i < Q; i++)
    {
        if(A[queries[i].b]-A[queries[i].a]==queries[i].c) ret+=queries[i].d;
    }
    return ret;    
}

int solve(){
    // vector<int> A(N,-1);
    // // A[queries[0].a] = 0;
    // cout << dfs(0,A) << endl;
    // return 0;
    // ll ans = 0;
    // for (int i = 1; i < N-1; i++)
    // {
    //     A[i] = min(A[i-1]+m,M-1);
    // // }
    // ans = max(ans,calc(A));
    // cout << ans << endl;
    vector<int> A(10,-1);
    ll ans=0;
    A[0] = 0;
    for (int i1 = 0 ; i1 < M; i1++)
    for (int i2 = i1; i2 < M; i2++)
    for (int i3 = i2; i3 < M; i3++)
    for (int i4 = i3; i4 < M; i4++)
    for (int i5 = i4; i5 < M; i5++)
    for (int i6 = i5; i6 < M; i6++)
    for (int i7 = i6; i7 < M; i7++)
    for (int i8 = i7; i8 < M; i8++)
    for (int i9 = i8; i9 < M; i9++)
    {
        A[1] = i1;
        A[2] = i2;
        A[3] = i3;
        A[4] = i4;
        A[5] = i5;
        A[6] = i6;
        A[7] = i7;
        A[8] = i8;
        A[9] = i9;
        ans = max(ans,calc(A));
    }
    cout << ans << endl;
    return 0;
}   

int main()
{
    input();
    solve();
    return 0;
}


Measured memory_bytes: 3636