import itertools



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

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



max_score = 0



for A in itertools.combinations_with_replacement(list(range(1, M + 1)), N):

    score = 0

    for a, b, c, d in ABCD:

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

            score += d

    max_score = max(max_score, score)



print(max_score)

import copy





def dfs(A):

    global ans

    if len(A) == N:

        score = 0

        for a, b, c, d in buff:

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

                score += d

        ans = max(ans, score)

        return



    A.append(A[-1])

    while A[-1] <= M:

        dfs(copy.copy(A))

        A[-1] += 1





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

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

ans = 0

dfs([1])

print(ans)

import itertools

N, M, Q = list(map(int, input().split()))
ABCD = [list(map(int, input().split())) for _ in range(Q)]
max_score = 0
for A in itertools.combinations_with_replacement(list(range(1, M + 1)), N):
    score = 0
    for a, b, c, d in ABCD:
        if A[b - 1] - A[a - 1] == c:
            score += d
    max_score = max(max_score, score)
print(max_score)

import copy


def dfs(A):
    global ans
    if len(A) == N:
        score = 0
        for a, b, c, d in buff:
            if A[b - 1] - A[a - 1] == c:
                score += d
        ans = max(ans, score)
        return
    A.append(A[-1])
    while A[-1] <= M:
        dfs(copy.copy(A))
        A[-1] += 1


N, M, Q = list(map(int, input().split()))
buff = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
dfs([1])
print(ans)

-import itertools +import copy + + +def dfs(A): +    global ans +    if len(A) == N: +        score = 0 +        for a, b, c, d in buff: +            if A[b - 1] - A[a - 1] == c: +                score += d +        ans = max(ans, score) +        return +    A.append(A[-1]) +    while A[-1] <= M: +        dfs(copy.copy(A)) +        A[-1] += 1 + -ABCD = [list(map(int, input().split())) for _ in range(Q)] -max_score = 0 -for A in itertools.combinations_with_replacement(list(range(1, M + 1)), N): -    score = 0 -    for a, b, c, d in ABCD: -        if A[b - 1] - A[a - 1] == c: -            score += d -    max_score = max(max_score, score) -print(max_score) +buff = [list(map(int, input().split())) for _ in range(Q)] +ans = 0 +dfs([1]) +print(ans)