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

int judge_point(int A[], int a[], int b[], int c[], int d[], int N, int M, int Q)
{
    int p = 0;
    for (size_t i = 0; i < Q; i++)
    {
        if (A[b[i] - 1] - A[a[i] - 1] == c[i])
        {
            p += d[i];
        }
    }
    return p;
}

int max(int a, int b)
{
    return a > b ? a : b;
}

int main(int argc, char const *argv[])
{
    int N, M, Q;
    int a[50], b[50], c[50], d[50];

    scanf("%d %d %d", &N, &M, &Q);
    for (size_t i = 0; i < Q; i++)
    {
        scanf("%d %d %d %d", &a[i], &b[i], &c[i], &d[i]);
    }

    int A_len = N;
    int A_num_max = M;
    int A[10];
    unsigned point = 0;

    for (size_t i0 = 1; i0 <= A_num_max; i0++)
        for (size_t i1 = i0; i1 <= A_num_max; i1++)
            for (size_t i2 = i1; i2 <= A_num_max; i2++)
                for (size_t i3 = i2; i3 <= A_num_max; i3++)
                    for (size_t i4 = i3; i4 <= A_num_max; i4++)
                        for (size_t i5 = i4; i5 <= A_num_max; i5++)
                            for (size_t i6 = i5; i6 <= A_num_max; i6++)
                                for (size_t i7 = i6; i7 <= A_num_max; i7++)
                                    for (size_t i8 = i7; i8 <= A_num_max; i8++)
                                        for (size_t i9 = i8; i9 <= A_num_max; i9++)
                                        {
                                            A[0] = i0;
                                            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;
                                            point = max(point, judge_point(A, a, b, c, d, N, M, Q));
                                        }

    printf("%u\n", point);
    
    return 0;
}
