from itertools import combinations_with_replacement



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

records = [tuple(map(int, input().split(' '))) for _ in range(Q)]



max_score = 0



for A in combinations_with_replacement(list(range(1, M + 1)), r=N):

    score = 0

    for a, b, c, d in records:

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

            score += d

    max_score = max(max_score, score)



print(max_score)

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

records = [tuple(map(int, input().split(' '))) for _ in range(Q)]



max_score = 0





def dfs(start, end, depth, numbers):

    if depth == N:

        global max_score

        for n in range(start, end + 1):

            A = (*numbers, n)

            score = 0

            for a, b, c, d in records:

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

                    score += d

            max_score = max(max_score, score)

    else:

        for n in range(start, end + 1):

            dfs(start=n, end=end, depth=depth + 1, numbers=(*numbers, n))





dfs(start=1, end=M, depth=1, numbers=())

print(max_score)

from itertools import combinations_with_replacement

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

N, M, Q = list(map(int, input().split(" ")))
records = [tuple(map(int, input().split(" "))) for _ in range(Q)]
max_score = 0


def dfs(start, end, depth, numbers):
    if depth == N:
        global max_score
        for n in range(start, end + 1):
            A = (*numbers, n)
            score = 0
            for a, b, c, d in records:
                if A[b - 1] - A[a - 1] == c:
                    score += d
            max_score = max(max_score, score)
    else:
        for n in range(start, end + 1):
            dfs(start=n, end=end, depth=depth + 1, numbers=(*numbers, n))


dfs(start=1, end=M, depth=1, numbers=())
print(max_score)

-from itertools import combinations_with_replacement - -for A in combinations_with_replacement(list(range(1, M + 1)), r=N): -    score = 0 -    for a, b, c, d in records: -        if A[b - 1] - A[a - 1] == c: -            score += d -    max_score = max(max_score, score) + + +def dfs(start, end, depth, numbers): +    if depth == N: +        global max_score +        for n in range(start, end + 1): +            A = (*numbers, n) +            score = 0 +            for a, b, c, d in records: +                if A[b - 1] - A[a - 1] == c: +                    score += d +            max_score = max(max_score, score) +    else: +        for n in range(start, end + 1): +            dfs(start=n, end=end, depth=depth + 1, numbers=(*numbers, n)) + + +dfs(start=1, end=M, depth=1, numbers=())