from itertools import combinations_with_replacement



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

ABCD = [list(map(int, input().split())) for i in range(Q)]

ans = 0



for x in combinations_with_replacement(list(range(M)), N):

    temp = 0

    for i in range(Q):

        if x[ABCD[i][1]-1]+1 == x[ABCD[i][0]-1]+1 + ABCD[i][2]:

            temp += ABCD[i][3]

    if temp > ans:

        ans = temp

print(ans)
from itertools import combinations_with_replacement



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

ABCD = [list(map(int, input().split())) for i in range(Q)]

ans = 0



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

    temp = 0

    for abcd in ABCD:

        if A[abcd[1]-1]+1 == A[abcd[0]-1]+1 + abcd[2]:

            temp += abcd[3]

    if temp > ans:

        ans = temp

print(ans)
from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))
ABCD = [list(map(int, input().split())) for i in range(Q)]
ans = 0
for x in combinations_with_replacement(list(range(M)), N):
    temp = 0
    for i in range(Q):
        if x[ABCD[i][1] - 1] + 1 == x[ABCD[i][0] - 1] + 1 + ABCD[i][2]:
            temp += ABCD[i][3]
    if temp > ans:
        ans = temp
print(ans)

from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))
ABCD = [list(map(int, input().split())) for i in range(Q)]
ans = 0
for A in combinations_with_replacement(list(range(M)), N):
    temp = 0
    for abcd in ABCD:
        if A[abcd[1] - 1] + 1 == A[abcd[0] - 1] + 1 + abcd[2]:
            temp += abcd[3]
    if temp > ans:
        ans = temp
print(ans)

-for x in combinations_with_replacement(list(range(M)), N): +for A in combinations_with_replacement(list(range(M)), N): -    for i in range(Q): -        if x[ABCD[i][1] - 1] + 1 == x[ABCD[i][0] - 1] + 1 + ABCD[i][2]: -            temp += ABCD[i][3] +    for abcd in ABCD: +        if A[abcd[1] - 1] + 1 == A[abcd[0] - 1] + 1 + abcd[2]: +            temp += abcd[3]