from itertools import combinations_with_replacement

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

abcd = []

for _ in range(q):

  a,b,c,d = list(map(int, input().split()))

  abcd.append([a,b,c,d])



nums = list(range(1,m+1))

l = list(combinations_with_replacement(nums, n))



ans = 0

for i in l:

  p = 0

  for j in abcd:

    b = i[j[1]-1]

    a = i[j[0]-1]

    if b - a == j[2]:

      p += j[3]

  ans = max(ans, p)

  

print(ans)
from itertools import combinations_with_replacement

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

abcd = []

for _ in range(q):

  a,b,c,d = list(map(int, input().split()))

  abcd.append([a,b,c,d])



nums = list(range(1,m+1))

l = list(combinations_with_replacement(nums, n))



ans = 0

for i in l:

  p = 0

  for j in abcd:

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

      p += j[3]

  ans = max(ans, p)

  

print(ans)
from itertools import combinations_with_replacement

n, m, q = list(map(int, input().split()))
abcd = []
for _ in range(q):
    a, b, c, d = list(map(int, input().split()))
    abcd.append([a, b, c, d])
nums = list(range(1, m + 1))
l = list(combinations_with_replacement(nums, n))
ans = 0
for i in l:
    p = 0
    for j in abcd:
        b = i[j[1] - 1]
        a = i[j[0] - 1]
        if b - a == j[2]:
            p += j[3]
    ans = max(ans, p)
print(ans)

from itertools import combinations_with_replacement

n, m, q = list(map(int, input().split()))
abcd = []
for _ in range(q):
    a, b, c, d = list(map(int, input().split()))
    abcd.append([a, b, c, d])
nums = list(range(1, m + 1))
l = list(combinations_with_replacement(nums, n))
ans = 0
for i in l:
    p = 0
    for j in abcd:
        if i[j[1] - 1] - i[j[0] - 1] == j[2]:
            p += j[3]
    ans = max(ans, p)
print(ans)

-        b = i[j[1] - 1] -        a = i[j[0] - 1] -        if b - a == j[2]: +        if i[j[1] - 1] - i[j[0] - 1] == j[2]: