import itertools

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

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

d=list(itertools.combinations_with_replacement(list(range(1,m+1)), n))

ans=0

count=0

for i in range(len(d)):

  for j in range(q):

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

      count+=abcd[j][3]

  if ans<count:

    ans=count

  count=0

print(ans)

  
import itertools

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

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

ans=0

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

  a=list(i)

  hantei=0

  for j in range(q):

    if (a[d[j][1]-1]-a[d[j][0]-1])==d[j][2]:

      hantei+=d[j][3]

  ans=max(ans,hantei)

print(ans)
import itertools

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

import itertools

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

-abcd = [list(map(int, input().split())) for i in range(q)] -d = list(itertools.combinations_with_replacement(list(range(1, m + 1)), n)) +d = [list(map(int, input().split())) for i in range(q)] -count = 0 -for i in range(len(d)): +for i in list(itertools.combinations_with_replacement(list(range(1, m + 1)), n)): +    a = list(i) +    hantei = 0 -        if abcd[j][2] == d[i][abcd[j][1] - 1] - d[i][abcd[j][0] - 1]: -            count += abcd[j][3] -    if ans < count: -        ans = count -    count = 0 +        if (a[d[j][1] - 1] - a[d[j][0] - 1]) == d[j][2]: +            hantei += d[j][3] +    ans = max(ans, hantei)