import itertools

N,M,Q = list(map(int,input().split()))

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

com = list(itertools.combinations_with_replacement(list(range(1,M+1)),N))

answer = 0

for i in com:

    ans = 0

    for j in A:

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

            ans += j[3]

    answer = max(answer,ans)

print(answer)
N,M,Query = list(map(int,input().split()))

from itertools import combinations_with_replacement

P = tuple(combinations_with_replacement(list(range(1,M+1)),N))

Q = []

for _ in range(Query):

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

    Q.append((a,b,c,d))

    answer = 0

for p in P:

    ans = 0

    for a,b,c,d in Q:

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

            ans += d

    answer = max(ans,answer)

print(answer)
import itertools

N, M, Q = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(Q)]
com = list(itertools.combinations_with_replacement(list(range(1, M + 1)), N))
answer = 0
for i in com:
    ans = 0
    for j in A:
        if i[j[1] - 1] - i[j[0] - 1] == j[2]:
            ans += j[3]
    answer = max(answer, ans)
print(answer)

N, M, Query = list(map(int, input().split()))
from itertools import combinations_with_replacement

P = tuple(combinations_with_replacement(list(range(1, M + 1)), N))
Q = []
for _ in range(Query):
    a, b, c, d = list(map(int, input().split()))
    Q.append((a, b, c, d))
    answer = 0
for p in P:
    ans = 0
    for a, b, c, d in Q:
        if p[b - 1] - p[a - 1] == c:
            ans += d
    answer = max(ans, answer)
print(answer)

-import itertools +N, M, Query = list(map(int, input().split())) +from itertools import combinations_with_replacement -N, M, Q = list(map(int, input().split())) -A = [list(map(int, input().split())) for _ in range(Q)] -com = list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)) -answer = 0 -for i in com: +P = tuple(combinations_with_replacement(list(range(1, M + 1)), N)) +Q = [] +for _ in range(Query): +    a, b, c, d = list(map(int, input().split())) +    Q.append((a, b, c, d)) +    answer = 0 +for p in P: -    for j in A: -        if i[j[1] - 1] - i[j[0] - 1] == j[2]: -            ans += j[3] -    answer = max(answer, ans) +    for a, b, c, d in Q: +        if p[b - 1] - p[a - 1] == c: +            ans += d +    answer = max(ans, answer)