#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;
using namespace std;

#define INF ((1<<30)-1)
#define LLINF (1LL<<60)
#define EPS (1e-10)

struct q {
    int a, b, c, d;
    void input() { cin >> a >> b >> c >> d; }
    bool operator<(const q& tmp) const {
        return d < tmp.d;
    }
};

ll ret;
int N, M, Q;
int a[101], b[101], c[101], d[101];

int ans[101];
void solve(int i, int ma=1) {
    if (i >= N) {
        // ok
        ll tmp = 0;
        rep(i, Q) {
            if (ans[b[i]-1] - ans[a[i]-1] == c[i]) tmp += d[i];
        }
        ret = max(ret, tmp);
        return;
    }

    for (int j=ma; j<=M; ++j) {
        ans[i] = j;
        solve(i+1, j);
        ans[i] = -1;
    }
    return;
}

int main() {
    cin >> N >> M >> Q;
    rep(i, Q) cin >> a[i] >> b[i] >> c[i] >> d[i];
    solve(0);
    cout << ret << endl;
}
