import itertools

n,m,q = list(map(int,input().split()))

target = [list(map(int,input().split())) for _ in range(q)]

ans = 0



for A in itertools.combinations_with_replacement(list(range(1,m+1)),n):

  count = 0

  for abcd in target:

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

      count += abcd[3]

  

  ans = max(ans,count)



print(ans)
import itertools

n,m,q = list(map(int,input().split()))

target = [list(map(int,input().split())) for _ in range(q)]

ans = 0

for A in itertools.combinations_with_replacement(list(range(1,m+1)),n):

  count = sum([abcd[3] for abcd in target if A[abcd[1]-1] - A[abcd[0]-1] == abcd[2]])

  ans = max(ans,count)

print(ans)
import itertools

n, m, q = list(map(int, input().split()))
target = [list(map(int, input().split())) for _ in range(q)]
ans = 0
for A in itertools.combinations_with_replacement(list(range(1, m + 1)), n):
    count = 0
    for abcd in target:
        if A[abcd[1] - 1] - A[abcd[0] - 1] == abcd[2]:
            count += abcd[3]
    ans = max(ans, count)
print(ans)

import itertools

n, m, q = list(map(int, input().split()))
target = [list(map(int, input().split())) for _ in range(q)]
ans = 0
for A in itertools.combinations_with_replacement(list(range(1, m + 1)), n):
    count = sum(
        [abcd[3] for abcd in target if A[abcd[1] - 1] - A[abcd[0] - 1] == abcd[2]]
    )
    ans = max(ans, count)
print(ans)

-    count = 0 -    for abcd in target: -        if A[abcd[1] - 1] - A[abcd[0] - 1] == abcd[2]: -            count += abcd[3] +    count = sum( +        [abcd[3] for abcd in target if A[abcd[1] - 1] - A[abcd[0] - 1] == abcd[2]] +    )