#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define vout(x) rep(i,x.size()) cout << x[i] << " "
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

struct data{int a, b, c, d;};
vector<data> Q;
int n, m, q;

int dfs(int v, int id, vint &x) {
    if (id == n) {
        int sum = 0;
        rep(i,q) {
            if (x[Q[i].b]-x[Q[i].a] == Q[i].c) sum += Q[i].d;
        }
        return sum;
    }
    int res = 0;
    x[id] = v;
    for (int i = v; i <= m; i++) {
        res = max(dfs(i,id+1,x),res);
    }
    return res;
}

int main() {
    cin >> n >> m >> q;
    Q.resize(q);
    vint x(n);
    rep(i,q) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        a--; b--;
        Q[i] = data{a,b,c,d};
    }
    int ans = 0;
    for (int i = 1; i <= m; i++) {
        ans = max(dfs(i,0,x),ans);
    }
    cout << ans << endl;
}