#include <stdio.h>

#define N 976543
#define SWAP(a, b) (a += b,b = a - b,a -= b)
long long int ans = 0;
int n, m, q;
int a[50], b[50], c[50], d[50];
int A[10];

int min(int i) {
    int s = 0;
    for (int j = 0; j < i; j++)
        if (A[j] != N && A[j] > s) s = A[j];
    return s;
}

void e(int i, long long int s) {
    if (i > q) {
        if (s > ans) ans = s;
        return;
    }
    if (A[b[i]] != N && A[a[i]] != N) {
        if (A[b[i]] - A[a[i]] == c[i]) {
            e(i + 1, s + d[i]);
            return;
        }
    } else if (A[b[i]] != N && A[a[i]] == N) {
        A[a[i]] = A[b[i]] - c[i];
        e(i + 1, s + d[i]);
        A[a[i]] = N;
    } else if (A[b[i]] == N && A[a[i]] != N) {
        A[b[i]] = A[a[i]] + c[i];
        e(i + 1, s + d[i]);
        A[b[i]] = N;
    } else {
        A[a[i]] = min(a[i]);
        A[b[i]] = A[a[i]] + c[i];
        e(i + 1, s + d[i]);
        A[b[i]] = A[a[i]] = N;
    }
    e(i + 1, s);
}

void sort() {
    for (int i = 0; i < q; i++)
        for (int j = i + 1; j < q; j++) {
            if (a[i] < a[j]) {
                SWAP(a[i], a[j]);
                SWAP(b[i], b[j]);
                SWAP(c[i], c[j]);
                SWAP(d[i], d[j]);
            }
        }
}

int main() {
    scanf("%d %d %d", &n, &m, &q);
    for (int i = 0; i < 10; i++)A[i] = N;
    for (int i = 0; i < q; i++) {
        scanf("%d %d %d %d", &a[i], &b[i], &c[i], &d[i]);
    }
    sort();
    e(0, 0);
    printf("%lld", ans);
    return 0;
}
