#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <sstream>
#include <cmath>
#include <functional>
#include <fstream>
#include <cstdlib>
typedef long long ll;
const ll MOD = 1e9+7;


#define REP(i, N) for (ll i = 0; i < (N); i++)
#define REPP(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define VSORT(c) sort((c).begin(),(c).end())
#define SZ(x) ((ll)(x).size())
//vvintを作る　マクロで　 #define vvint(N,M) vector<vector<ll>>

using namespace std;
// 変数初期化
vector<ll> bsv; // binary_search用のvector
typedef pair<ll, ll> Pll;
vector<Pll> facts; // 素因数分解の結果格納用(基数:乗数)
vector<vector<ll> > bit_search_v; //nit全探索の結果格納用

// index が条件を満たすかどうか
bool isOK(ll index, ll key) {
    if (bsv[index] >= key) return true;
    else return false;
}

// 汎用的な二分探索のテンプレ
ll binary_search(int key) {
    int ng = -1;
    int ok = (ll)bsv.size();

    /* ok と ng のどちらが大きいかわからないことを考慮 */
    while (abs(ok - ng) > 1) {
        int mid = (ok + ng) / 2;

        if (isOK(mid, key)) ok = mid;
        else ng = mid;
    }
    // OK(key以上となる)のindex番号を返す
    return ok;
}

// 最大公約数
int gcd(int a, int b) {
    if (b==0) return a;
    else return gcd(b, a%b);
}

// 最小公倍数
int lcm(int a, int b) {
    return a * b / gcd(a, b);
}

//　素因数分解
void prime_fact(ll target){
    ll base=2, exp=0;
    while (base*base<=target) {
        while (target%base==0) {
            target /= base; exp++;
        }
        if (exp>0) {
            facts.push_back(make_pair(base, exp));
        }
        base++; exp=0;
    }
    if (target>1) facts.push_back(make_pair(target,1));
}
// prime_factの結果の確認用
void print_facts(){
    for (int i=0; i<facts.size(); i++) {
        cout << facts[i].first << " " << facts[i].second << endl;
    }
}

// bit全探索
void bit_search(ll target, ll m){
    for (ll i=0; i<(1<<target); ++i) {
        if (i==0) continue;
        vector<ll> v;
        for (ll j=0; j<target; ++j) {
            if (i&(1<<j)) v.push_back(j+1);
        }
        if(v.size()== m) bit_search_v.push_back(v);
    }
}
// bit全探索の結果確認用
void print_bit_search(){
    for (ll i=0; i<bit_search_v.size(); ++i) {
        for (ll j=0; j<bit_search_v[i].size(); ++j) {
            cout << bit_search_v[i][j] << " ";
        }
        cout << endl;
    }
}
void print_v(vector<ll> vll){
    for(ll i = 0; i < vll.size();i++){
        cout << vll[i] << " : ";
    }
    cout << endl;
}
void print_vv(vector<vector<ll> > vvll){
    for(ll i = 0; i < vvll.size();i++){
        for(ll l = 0; l < vvll[i].size();l++){
            cout << vvll[i][l] << " : ";
        }
    }
    cout << endl;
}

void dps(ll partition,ll cnt, ll n, ll m, vector<ll> tv){
    if (n == tv.size()) {
        bit_search_v.push_back(tv);
        return;
    }
    for(ll i = cnt; i < m+1;i++){
        vector<ll> v;
        for(ll l = 0; l < tv.size();l++){
            v.push_back(tv[l]);
        }
        v.push_back(i);
        dps(partition,i,n,m,v);
    }

}



int main(){
    
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n,m,q;
    cin >> n >> m >> q;
    vector<ll> a(q),b(q),c(q),d(q);

    ll ans=0,tmp = 0;
    for(ll i = 0; i < q;i++){
        cin >> a[i] >> b[i] >> c[i] >> d[i];
    }
    vector<ll> tv;
    dps(0,1,n,m,tv);
    for(ll i = 0; i < bit_search_v.size();i++){
        for(ll l = 0; l < q;l++){
            if(bit_search_v[i][b[l]-1]-bit_search_v[i][a[l]-1] == c[l]){
                tmp += d[l];
            }
        }
        ans = max(ans,tmp);
        tmp = 0;
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 13444