#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;});    
}


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;    
}

ll dfs(int i, vector<int> A){
    if(i==0) return dfs(1,A);
    if(i==N) return calc(A);
    ll ret = 0;
    for (int j = A[i-1]; j < M; j++)
    {
        A[i] = j;
        ret = max(ret, dfs(i+1,A));
    }
    return ret;    
}

int solve_dfs(){
    vector<int> A(N,0);
    cout << dfs(0,A) << endl;
    return 0;
}

int solve(){
    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_dfs();
    return 0;
}


Measured memory_bytes: 3660