import itertools

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

P = [[int(x) for x in input().split()] for _ in range(Q)]



li = [x for x in range(1, M+1)]

Com = itertools.combinations_with_replacement(li, N)



ans=0

for com in Com:

    reg=0

    for a,b,c,d in P:

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

            reg += d

    ans = max(reg, ans)

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

ABCD = [[int(x) for x in input().split()] for _ in range(Q)]





def dfs(A):

    if len(A)==N:

        return calc(A)

    

    res = 0

    s = A[-1] if len(A)>0 else 1

    for i in range(s, M+1):

        A.append(i)

        res = max(res, dfs(A))

        A.pop()

    return res

        

    

def calc(A):

    score = 0

    for a,b,c,d in ABCD:

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

            score += d

    return score





ans = dfs([])

print(ans)
import itertools

N, M, Q = list(map(int, input().split()))
P = [[int(x) for x in input().split()] for _ in range(Q)]
li = [x for x in range(1, M + 1)]
Com = itertools.combinations_with_replacement(li, N)
ans = 0
for com in Com:
    reg = 0
    for a, b, c, d in P:
        if com[b - 1] - com[a - 1] == c:
            reg += d
    ans = max(reg, ans)
print(ans)

N, M, Q = list(map(int, input().split()))
ABCD = [[int(x) for x in input().split()] for _ in range(Q)]


def dfs(A):
    if len(A) == N:
        return calc(A)
    res = 0
    s = A[-1] if len(A) > 0 else 1
    for i in range(s, M + 1):
        A.append(i)
        res = max(res, dfs(A))
        A.pop()
    return res


def calc(A):
    score = 0
    for a, b, c, d in ABCD:
        if A[b - 1] - A[a - 1] == c:
            score += d
    return score


ans = dfs([])
print(ans)

-import itertools +N, M, Q = list(map(int, input().split())) +ABCD = [[int(x) for x in input().split()] for _ in range(Q)] -N, M, Q = list(map(int, input().split())) -P = [[int(x) for x in input().split()] for _ in range(Q)] -li = [x for x in range(1, M + 1)] -Com = itertools.combinations_with_replacement(li, N) -ans = 0 -for com in Com: -    reg = 0 -    for a, b, c, d in P: -        if com[b - 1] - com[a - 1] == c: -            reg += d -    ans = max(reg, ans) + +def dfs(A): +    if len(A) == N: +        return calc(A) +    res = 0 +    s = A[-1] if len(A) > 0 else 1 +    for i in range(s, M + 1): +        A.append(i) +        res = max(res, dfs(A)) +        A.pop() +    return res + + +def calc(A): +    score = 0 +    for a, b, c, d in ABCD: +        if A[b - 1] - A[a - 1] == c: +            score += d +    return score + + +ans = dfs([])