#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <queue>
#include <vector>
#define rep(i, a, n) for(int i = a; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main()
{
    int n, m, q;
    cin >> n >> m >> q;
    vector<int> x(10, -1001001001);
    ll ans = 0;
    vector<int> a(q), b(q), c(q), d(q);
    rep(i, 0, q){
        cin >> a[i] >> b[i] >> c[i] >> d[i];
    }
    rep(i1, 1, m+1){
        x[0] = i1;
        rep(i2, i1, m+1){
            x[1] = i2;
            if(n == 2){
                ll res = 0;
                rep(j, 0, q){
                    if(x[b[j]-1] - x[a[j]-1] == c[j]){
                        res += d[j];
                    }
                }
                ans = max(ans, res);
            }
            rep(i3, i2, m+1){
                x[2] = i3;
                if(n == 3){
                    ll res = 0;
                    rep(j, 0, q){
                        if(x[b[j]-1] - x[a[j]-1] == c[j]){
                            res += d[j];
                        }
                    }
                    ans = max(ans, res);
                }
                rep(i4, i3, m+1){
                    x[3] = i4;
                    if(n == 4){
                        ll res = 0;
                        rep(j, 0, q){
                            if(x[b[j]-1] - x[a[j]-1] == c[j]){
                                res += d[j];
                            }
                        }
                        ans = max(ans, res);
                    }
                    rep(i5, i4, m+1){
                        x[4] = i5;
                        if(n == 5){
                            ll res = 0;
                            rep(j, 0, q){
                                if(x[b[j]-1] - x[a[j]-1] == c[j]){
                                    res += d[j];
                                }
                            }
                            ans = max(ans, res);
                        }
                        rep(i6, i5, m+1){
                            x[5] = i6;
                            if(n == 6){
                                ll res = 0;
                                rep(j, 0, q){
                                    if(x[b[j]-1] - x[a[j]-1] == c[j]){
                                        res += d[j];
                                    }
                                }
                                ans = max(ans, res);
                            }
                            rep(i7, i6, m+1){
                                x[6] = i7;
                                if(n == 7){
                                    ll res = 0;
                                    rep(j, 0, q){
                                        if(x[b[j]-1] - x[a[j]-1] == c[j]){
                                            res += d[j];
                                        }
                                    }
                                    ans = max(ans, res);
                                }
                                rep(i8, i7, m+1){
                                    x[7] = i8;
                                    if(n == 8){
                                        ll res = 0;
                                        rep(j, 0, q){
                                            if(x[b[j]-1] - x[a[j]-1] == c[j]){
                                                res += d[j];
                                            }
                                        }
                                        ans = max(ans, res);
                                    }
                                    rep(i9, i8, m+1){
                                        x[8] = i9;
                                        if(n == 9){
                                            ll res = 0;
                                            rep(j, 0, q){
                                                if(x[b[j]-1] - x[a[j]-1] == c[j]){
                                                    res += d[j];
                                                }
                                            }
                                            ans = max(ans, res);
                                        }
                                        rep(i10, i9, m+1){
                                            x[9] = i10;
                                            if(n == 10){
                                                ll res = 0;
                                                rep(j, 0, q){
                                                    if(x[b[j]-1] - x[a[j]-1] == c[j]){
                                                        res += d[j];
                                                    }
                                                }
                                                ans = max(ans, res);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3636