from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))

l = [tuple(map(int, input().split())) for _ in range(Q)]

ans = 0

for A in combinations_with_replacement(list(range(1, M+1)), N):

    v = 0

    for a, b, c, d in l:

        if A[b-1]-A[a-1]==c:

            v += d

    ans = max(ans, v)

print(ans)

from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))

l = [tuple(map(int, input().split())) for _ in range(Q)]

ans = 0

for A in combinations_with_replacement(list(range(1, M+1)), N):

    ans = max(ans, sum(d for a, b, c, d in l if A[b-1]-A[a-1]==c))

print(ans)

from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))
l = [tuple(map(int, input().split())) for _ in range(Q)]
ans = 0
for A in combinations_with_replacement(list(range(1, M + 1)), N):
    v = 0
    for a, b, c, d in l:
        if A[b - 1] - A[a - 1] == c:
            v += d
    ans = max(ans, v)
print(ans)

from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))
l = [tuple(map(int, input().split())) for _ in range(Q)]
ans = 0
for A in combinations_with_replacement(list(range(1, M + 1)), N):
    ans = max(ans, sum(d for a, b, c, d in l if A[b - 1] - A[a - 1] == c))
print(ans)

-    v = 0 -    for a, b, c, d in l: -        if A[b - 1] - A[a - 1] == c: -            v += d -    ans = max(ans, v) +    ans = max(ans, sum(d for a, b, c, d in l if A[b - 1] - A[a - 1] == c))