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

int A[10], Req[50][4];
int N, M, Q;

int score(int idx) {
    int i, v, max = 0;
    if (idx == N) {
        for (i = 0; i < Q; i++) {
            if (A[Req[i][1] - 1] - A[Req[i][0] - 1] == Req[i][2]) max += Req[i][3];
        }
        return max;
    }
    v = idx ? A[idx - 1] : 1;
    while (v <= M) {
        A[idx] = v;
        int temp = score(idx + 1);
        if (max < temp) max = temp;
        v++;
    }
    return max;
}

int main() {
    int i, j, max = 0;
    scanf("%d %d %d", &N, &M, &Q);
    for (i = 0; i < Q; i++)
        for (j = 0; j < 4; j++)
            scanf("%d", &Req[i][j]);

    printf("%d\n", score(0));
    return 0;
}