from itertools import combinations_with_replacement



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

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



ans = 0

for seq in combinations_with_replacement(list(range(1, m + 1)), n):

    score = 0

    for a, b, c, d in req:

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

            score += d

    ans = max(ans, score)



print(ans)

from collections import deque





def calc(nums):

    ret = 0

    for a, b, c, d in req:

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

            ret += d



    return ret





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

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



ans = 0



que = deque()



# [1]～[m]までキューに追加します。実は[1]だけでもいいです。考えてみてください。

for i in range(1, m + 1):

    que.append([i])



while que:

    seq = que.pop()



    if len(seq) == n:

        score = calc(seq)

        ans = max(ans, score)

    else:

        for i in range(seq[-1], m + 1):

            seq_next = seq + [i]

            que.append(seq_next)



print(ans)

from itertools import combinations_with_replacement

n, m, q = list(map(int, input().split()))
req = [list(map(int, input().split())) for _ in range(q)]
ans = 0
for seq in combinations_with_replacement(list(range(1, m + 1)), n):
    score = 0
    for a, b, c, d in req:
        if seq[b - 1] - seq[a - 1] == c:
            score += d
    ans = max(ans, score)
print(ans)

from collections import deque


def calc(nums):
    ret = 0
    for a, b, c, d in req:
        if nums[b - 1] - nums[a - 1] == c:
            ret += d
    return ret


n, m, q = list(map(int, input().split()))
req = [list(map(int, input().split())) for _ in range(q)]
ans = 0
que = deque()
# [1]～[m]までキューに追加します。実は[1]だけでもいいです。考えてみてください。
for i in range(1, m + 1):
    que.append([i])
while que:
    seq = que.pop()
    if len(seq) == n:
        score = calc(seq)
        ans = max(ans, score)
    else:
        for i in range(seq[-1], m + 1):
            seq_next = seq + [i]
            que.append(seq_next)
print(ans)

-from itertools import combinations_with_replacement +from collections import deque + + +def calc(nums): +    ret = 0 +    for a, b, c, d in req: +        if nums[b - 1] - nums[a - 1] == c: +            ret += d +    return ret + -for seq in combinations_with_replacement(list(range(1, m + 1)), n): -    score = 0 -    for a, b, c, d in req: -        if seq[b - 1] - seq[a - 1] == c: -            score += d -    ans = max(ans, score) +que = deque() +# [1]～[m]までキューに追加します。実は[1]だけでもいいです。考えてみてください。 +for i in range(1, m + 1): +    que.append([i]) +while que: +    seq = que.pop() +    if len(seq) == n: +        score = calc(seq) +        ans = max(ans, score) +    else: +        for i in range(seq[-1], m + 1): +            seq_next = seq + [i] +            que.append(seq_next)