# C - Many Requirements



import itertools



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

TEST = [0] * Q

for i in range(Q):

    TEST[i] = list(int(x) for x in input().split())



nums = [i+1 for i in range(M)]



ans = 0

for l in list(itertools.combinations_with_replacement(nums, N)):

    tmp = 0

    for t in TEST:

        if l[t[1]-1]-l[t[0]-1] ==t[2]:

            tmp += t[3]

        else:

            continue

    ans = max(ans, tmp)



print(ans)
# C - Many Requirements

# DFSで解く



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

A = [0] * Q

B = [0] * Q

C = [0] * Q

D = [0] * Q

for i in range(Q):

    A[i], B[i], C[i], D[i] = list(map(int, input().split()))



def dfs(L):

    global ans

    if len(L)==N+1:

        tmp = 0

        for i in range(Q):

            if L[B[i]] - L[A[i]] == C[i]:

                tmp += D[i]

        ans = max(tmp, ans)

        return

    else:

        for j in range(L[-1], M+1):

            dfs(L+[j])

    return ans



ans = 0

print((dfs([1])))
# C - Many Requirements
import itertools

N, M, Q = list(map(int, input().split()))
TEST = [0] * Q
for i in range(Q):
    TEST[i] = list(int(x) for x in input().split())
nums = [i + 1 for i in range(M)]
ans = 0
for l in list(itertools.combinations_with_replacement(nums, N)):
    tmp = 0
    for t in TEST:
        if l[t[1] - 1] - l[t[0] - 1] == t[2]:
            tmp += t[3]
        else:
            continue
    ans = max(ans, tmp)
print(ans)

# C - Many Requirements
# DFSで解く
N, M, Q = list(map(int, input().split()))
A = [0] * Q
B = [0] * Q
C = [0] * Q
D = [0] * Q
for i in range(Q):
    A[i], B[i], C[i], D[i] = list(map(int, input().split()))


def dfs(L):
    global ans
    if len(L) == N + 1:
        tmp = 0
        for i in range(Q):
            if L[B[i]] - L[A[i]] == C[i]:
                tmp += D[i]
        ans = max(tmp, ans)
        return
    else:
        for j in range(L[-1], M + 1):
            dfs(L + [j])
    return ans


ans = 0
print((dfs([1])))

-import itertools +# DFSで解く +N, M, Q = list(map(int, input().split())) +A = [0] * Q +B = [0] * Q +C = [0] * Q +D = [0] * Q +for i in range(Q): +    A[i], B[i], C[i], D[i] = list(map(int, input().split())) -N, M, Q = list(map(int, input().split())) -TEST = [0] * Q -for i in range(Q): -    TEST[i] = list(int(x) for x in input().split()) -nums = [i + 1 for i in range(M)] + +def dfs(L): +    global ans +    if len(L) == N + 1: +        tmp = 0 +        for i in range(Q): +            if L[B[i]] - L[A[i]] == C[i]: +                tmp += D[i] +        ans = max(tmp, ans) +        return +    else: +        for j in range(L[-1], M + 1): +            dfs(L + [j]) +    return ans + + -for l in list(itertools.combinations_with_replacement(nums, N)): -    tmp = 0 -    for t in TEST: -        if l[t[1] - 1] - l[t[0] - 1] == t[2]: -            tmp += t[3] -        else: -            continue -    ans = max(ans, tmp) -print(ans) +print((dfs([1])))