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

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



A = []

for i in range(1,M+1):

    A.append(i)



B = []

import itertools

for i in itertools.combinations_with_replacement(A,N):

    B.append(i)



C = []

for i in range(len(B)):

    score = 0

    for j in range(len(Q)):

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

            score += Q[j][3]

        C.append(score)



print((max(C)))
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)]  # 数字複数列、複数行
A = []
for i in range(1, M + 1):
    A.append(i)
B = []
import itertools

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

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)))

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