from itertools import combinations_with_replacement

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

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



num = list(combinations_with_replacement(list(range(1,m+1)), n))



ans = 0

for i in num:

  t = 0

  for j in abcd:

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

      t += j[3]

  ans = max(ans, t)

  

print(ans)
from itertools import combinations_with_replacement

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

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



ans = 0

for i in combinations_with_replacement(list(range(1,m+1)),n):

  t = 0

  for j in abcd:

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

      t += j[3]

  ans = max(ans, t)

print(ans)
from itertools import combinations_with_replacement

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

from itertools import combinations_with_replacement

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

-num = list(combinations_with_replacement(list(range(1, m + 1)), n)) -for i in num: +for i in combinations_with_replacement(list(range(1, m + 1)), n):