from itertools import combinations_with_replacement

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

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



ans = 0

for pattern in combinations_with_replacement(list(range(1, M + 1)), N):

    tmp_ans = 0

    pattern = list(pattern)

    for a, b, c, d in X:

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

            tmp_ans += d

    ans = max(ans, tmp_ans)



print(ans)

from itertools import combinations_with_replacement

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

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

ans = 0



def calc(arr):

    score = 0

    for a, b, c, d in X:

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

            score += d

    return score





def dfs(arr):

    global ans

    if len(arr) == N:

        ans = max(ans, calc(arr))



    elif len(arr) == 0:

        for nx in range(1, M + 1):

            dfs(arr + [nx])



    else:

        for nx in range(arr[-1], M + 1):

            dfs(arr + [nx])





dfs([])

print(ans)

from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))
X = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for pattern in combinations_with_replacement(list(range(1, M + 1)), N):
    tmp_ans = 0
    pattern = list(pattern)
    for a, b, c, d in X:
        if pattern[b - 1] - pattern[a - 1] == c:
            tmp_ans += d
    ans = max(ans, tmp_ans)
print(ans)

from itertools import combinations_with_replacement

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


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


def dfs(arr):
    global ans
    if len(arr) == N:
        ans = max(ans, calc(arr))
    elif len(arr) == 0:
        for nx in range(1, M + 1):
            dfs(arr + [nx])
    else:
        for nx in range(arr[-1], M + 1):
            dfs(arr + [nx])


dfs([])
print(ans)

-for pattern in combinations_with_replacement(list(range(1, M + 1)), N): -    tmp_ans = 0 -    pattern = list(pattern) + + +def calc(arr): +    score = 0 -        if pattern[b - 1] - pattern[a - 1] == c: -            tmp_ans += d -    ans = max(ans, tmp_ans) +        if arr[b - 1] - arr[a - 1] == c: +            score += d +    return score + + +def dfs(arr): +    global ans +    if len(arr) == N: +        ans = max(ans, calc(arr)) +    elif len(arr) == 0: +        for nx in range(1, M + 1): +            dfs(arr + [nx]) +    else: +        for nx in range(arr[-1], M + 1): +            dfs(arr + [nx]) + + +dfs([])