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

Q = [list(map(int, input().split())) for i in range(Q)]  #数字複数列、複数行



import itertools

A = []

for i in itertools.combinations_with_replacement(list(range(M)),N):

    score = 0

    for j in range(len(Q)):

        if i[Q[j][1]-1] - i[Q[j][0]-1] == Q[j][2]:

            score += Q[j][3]

        A.append(score)



print((max(A)))
N, M, Q = list(map(int, input().split()))  #数字

Q = [list(map(int, input().split())) for i in range(Q)]  #数字複数列、複数行



import itertools

A = 0

for i in itertools.combinations_with_replacement(list(range(M)),N):

    score = 0

    for j in range(len(Q)):

        if i[Q[j][1]-1] - i[Q[j][0]-1] == Q[j][2]:

            score += Q[j][3]

        if score > A:

            A = score



print(A)
N, M, Q = list(map(int, input().split()))  # 数字
Q = [list(map(int, input().split())) for i in range(Q)]  # 数字複数列、複数行
import itertools

A = []
for i in itertools.combinations_with_replacement(list(range(M)), N):
    score = 0
    for j in range(len(Q)):
        if i[Q[j][1] - 1] - i[Q[j][0] - 1] == Q[j][2]:
            score += Q[j][3]
        A.append(score)
print((max(A)))

N, M, Q = list(map(int, input().split()))  # 数字
Q = [list(map(int, input().split())) for i in range(Q)]  # 数字複数列、複数行
import itertools

A = 0
for i in itertools.combinations_with_replacement(list(range(M)), N):
    score = 0
    for j in range(len(Q)):
        if i[Q[j][1] - 1] - i[Q[j][0] - 1] == Q[j][2]:
            score += Q[j][3]
        if score > A:
            A = score
print(A)

-A = [] +A = 0 -        A.append(score) -print((max(A))) +        if score > A: +            A = score +print(A)