#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int N, M, Q, debug = 0;

void zeroclear(int *p);
void oneclear(int *p);
void of(int *p);
int hadd(const int *p);
void genA(const int *ind, int *As);
void next(int *ind);

int main(void)
{
    int cc, score = 0, temp, incind;
    int *a, *b, *c, *d, *ind, *As;
    scanf("%d %d %d", &N, &M, &Q);
    a = (int *)malloc(sizeof(int) * Q);
    b = (int *)malloc(sizeof(int) * Q);
    c = (int *)malloc(sizeof(int) * Q);
    d = (int *)malloc(sizeof(int) * Q);
    ind = (int *)malloc(sizeof(int) * N);
    As = (int *)malloc(sizeof(int) * N);
    zeroclear(ind);
    oneclear(As);
    for (cc = 0; cc < Q; ++cc)
    {
        scanf("%d %d %d %d", a + cc, b + cc, c + cc, d + cc);
    }
    while (1)
    {
        temp = 0;
        genA(ind, As);
        if (ind[N - 1] >= M)
        {
            break;
        }
        for (cc = 0; cc < Q; ++cc)
        {
            if (As[b[cc] - 1] - As[a[cc] - 1] == c[cc])
            {
                temp += d[cc];
            }
        }
        if (temp > score)
        {
            score = temp;
        }
        ++ind[0];
        of(ind);
        if (hadd(ind) > M - 1)
        {
            next(ind);
        }
        ++debug;
    }
    printf("%d", score);
    free(a);
    free(b);
    free(c);
    free(d);
    return 0;
}

void zeroclear(int *p)
{
    int cc;
    for (cc = 0; cc < N; ++cc)
    {
        p[cc] = 0;
    }
}

void oneclear(int *p)
{
    int cc;
    for (cc = 0; cc < N; ++cc)
    {
        p[cc] = 1;
    }
}

void of(int *p)
{
    int cc;
    for (cc = 0; cc < N - 1; ++cc)
    {
        if (p[cc] > M - 1)
        {
            p[cc] = 0;
            ++p[cc + 1];
        }
    }
}

int hadd(const int *p)
{
    int cc, ans = 0;
    for (cc = 0; cc < N; ++cc)
    {
        ans += p[cc];
    }
    return ans;
}

void genA(const int *ind, int *As)
{
    int cc;
    oneclear(As);
    As[0] += ind[0];
    for (cc = 1; cc < N; ++cc)
    {
        As[cc] = As[cc - 1];
        As[cc] += ind[cc];
    }
}

void next(int *ind)
{
    int cc;
    int *temp;
    temp = (int *)malloc(sizeof(int) * N);
    for (cc = 0; cc < N - 1; ++cc)
    {
        memcpy(temp, ind, sizeof(int) * N);
        temp[cc] = 0;
        ind[cc] = 0;
        ++temp[cc + 1];
        of(temp);
        if (hadd(temp) <= M - 1)
        {
            memcpy(ind, temp, sizeof(int) * N);
            break;
        }
    }
    memcpy(ind, temp, sizeof(int) * N);
    free(temp);
}