from itertools import combinations_with_replacement



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

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



mx = 0

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

	score = 0

	for a, b, c, d in abcdn:

		score += d if AN[b-1] - AN[a-1] == c else 0

	mx = max(score, mx)

print(mx)
from itertools import combinations_with_replacement



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

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



print((max([sum(d if AN[b-1] - AN[a-1] == c else 0 for a, b, c, d in abcdn) for AN in combinations_with_replacement(list(range(1, M+1)), N)])))
from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))
abcdn = [tuple(map(int, input().split())) for _ in range(Q)]
mx = 0
for AN in combinations_with_replacement(list(range(1, M + 1)), N):
    score = 0
    for a, b, c, d in abcdn:
        score += d if AN[b - 1] - AN[a - 1] == c else 0
    mx = max(score, mx)
print(mx)

from itertools import combinations_with_replacement

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

-mx = 0 -for AN in combinations_with_replacement(list(range(1, M + 1)), N): -    score = 0 -    for a, b, c, d in abcdn: -        score += d if AN[b - 1] - AN[a - 1] == c else 0 -    mx = max(score, mx) -print(mx) +print( +    ( +        max( +            [ +                sum(d if AN[b - 1] - AN[a - 1] == c else 0 for a, b, c, d in abcdn) +                for AN in combinations_with_replacement(list(range(1, M + 1)), N) +            ] +        ) +    ) +)