import itertools



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

l = []

ans = 0

ref = 0



for i in range(q):

    abcd = list(map(int, input().split()))

    l.append(abcd)



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

    for j in range(q):

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

            ref += l[j][3]

    ans = max(ans, ref)    

    ref = 0



print(ans)
import itertools



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

l = []

ans = 0

ref = 0



for i in range(q):

    abcd = list(map(int, input().split()))

    l.append(abcd)



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

    for a, b, c, d in l:

        if i[b - 1] - i[a - 1] == c:

            ref += d

    ans = max(ans, ref)

    ref = 0



print(ans)
import itertools

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

import itertools

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

-    for j in range(q): -        if i[l[j][1] - 1] - i[l[j][0] - 1] == l[j][2]: -            ref += l[j][3] +    for a, b, c, d in l: +        if i[b - 1] - i[a - 1] == c: +            ref += d