#include <bits/stdc++.h>
#define ll long long int
#define mod_ceil(a, b) (a % b) ? a / b + 1 : a / b
using namespace std;
struct quad
{
    ll a, b, c, d;
} Quad;

ll n, m, q;
vector<quad> Q;

ll calc_score(vector<ll> A)
{
    ll score = 0;
    for (auto i : Q)
    {
        if (i.a <= n and i.b <= n)
        {
            if (A[i.b - 1] - A[i.a - 1] == i.c)
            {
                score += i.d;
            }
        }
    }
    return score;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n >> m >> q;
    for (ll i = 0; i < q; i++)
    {
        cin >> Quad.a >> Quad.b >> Quad.c >> Quad.d;
        Q.push_back(Quad);
    }
    ll final_score = 0;
    vector<ll> tmp;

    for (int i1 = 1; i1 <= m; i1++)
    {
        tmp.push_back(i1);
        for (int i2 = i1; i2 <= m; i2++)
        {
            tmp.push_back(i2);
            if (n == 2)
                final_score = max(final_score, calc_score(tmp));
            for (int i3 = i2; i3 <= m; i3++)
            {
                tmp.push_back(i3);
                if (n == 3)
                    final_score = max(final_score, calc_score(tmp));
                for (int i4 = i3; i4 <= m; i4++)
                {
                    tmp.push_back(i4);
                    if (n == 4)
                        final_score = max(final_score, calc_score(tmp));
                    for (int i5 = i4; i5 <= m; i5++)
                    {
                        tmp.push_back(i5);
                        if (n == 5)
                            final_score = max(final_score, calc_score(tmp));
                        for (int i6 = i5; i6 <= m; i6++)
                        {
                            tmp.push_back(i6);
                            if (n == 6)
                                final_score = max(final_score, calc_score(tmp));
                            for (int i7 = i6; i7 <= m; i7++)
                            {
                                tmp.push_back(i7);
                                if (n == 7)
                                    final_score = max(final_score, calc_score(tmp));
                                for (int i8 = i7; i8 <= m; i8++)
                                {
                                    tmp.push_back(i8);
                                    if (n == 8)
                                        final_score = max(final_score, calc_score(tmp));
                                    for (int i9 = i8; i9 <= m; i9++)
                                    {
                                        tmp.push_back(i9);
                                        if (n == 9)
                                            final_score = max(final_score, calc_score(tmp));
                                        for (int i10 = i9; i10 <= m; i10++)
                                        {
                                            tmp.push_back(i10);
                                            if (n == 10)
                                                final_score = max(final_score, calc_score(tmp));
                                            tmp.pop_back();
                                        }
                                        tmp.pop_back();
                                    }
                                    tmp.pop_back();
                                }
                                tmp.pop_back();
                            }
                            tmp.pop_back();
                        }
                        tmp.pop_back();
                    }
                    tmp.pop_back();
                }
                tmp.pop_back();
            }
            tmp.pop_back();
        }
        tmp.pop_back();
    }
    cout << final_score << endl;
}

Measured memory_bytes: 3600