from itertools import combinations_with_replacement as comb_rplc

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

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



ans = 0



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

    now = 0

    for a, b, c, d in req:

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

            now += d

    ans = max(ans, now)



print(ans)
from _collections import deque



def cacl(seq):

    score = 0

    for a, b, c, d in req:

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

            score += d

    return score



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

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



ans = 0

que = deque()



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

    que.append([i])



while que:

    seq = que.popleft()



    if len(seq) == n:

        score = cacl(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 as comb_rplc

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

from _collections import deque


def cacl(seq):
    score = 0
    for a, b, c, d in req:
        if seq[b - 1] - seq[a - 1] == c:
            score += d
    return score


n, m, q = list(map(int, input().split()))
req = [list(map(int, input().split())) for _ in range(q)]
ans = 0
que = deque()
for i in range(1, m + 1):
    que.append([i])
while que:
    seq = que.popleft()
    if len(seq) == n:
        score = cacl(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 as comb_rplc +from _collections import deque + + +def cacl(seq): +    score = 0 +    for a, b, c, d in req: +        if seq[b - 1] - seq[a - 1] == c: +            score += d +    return score + -for seq in comb_rplc(list(range(1, m + 1)), n): -    now = 0 -    for a, b, c, d in req: -        if seq[b - 1] - seq[a - 1] == c: -            now += d -    ans = max(ans, now) +que = deque() +for i in range(1, m + 1): +    que.append([i]) +while que: +    seq = que.popleft() +    if len(seq) == n: +        score = cacl(seq) +        ans = max(ans, score) +    else: +        for i in range(seq[-1], m + 1): +            seq_next = seq + [i] +            que.append(seq_next)