from itertools import combinations_with_replacement



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

a = [0] * q

b = [0] * q

c = [0] * q

d = [0] * q

ans = 0



for i in range(q):

    a[i], b[i], c[i], d[i] = list(map(int, input().split()))



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

    now = 0

    for j in range(q):

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

            now += d[j]

    ans = max(ans, now)



print(ans)

def dfs(A):

    global ans

    if len(A) == n + 1:

        now = 0

        for j in range(q):

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

                now += d[j]

        ans = max(ans, now)

        return



    B = list(A)

    B.append(A[-1])

    while B[-1] <= m:

        dfs(tuple(B))

        B[-1] += 1





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

a = [0] * q

b = [0] * q

c = [0] * q

d = [0] * q

ans = 0



for i in range(q):

    a[i], b[i], c[i], d[i] = list(map(int, input().split()))



dfs(tuple([1]))



print(ans)

from itertools import combinations_with_replacement

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

def dfs(A):
    global ans
    if len(A) == n + 1:
        now = 0
        for j in range(q):
            if A[b[j]] - A[a[j]] == c[j]:
                now += d[j]
        ans = max(ans, now)
        return
    B = list(A)
    B.append(A[-1])
    while B[-1] <= m:
        dfs(tuple(B))
        B[-1] += 1


n, m, q = list(map(int, input().split()))
a = [0] * q
b = [0] * q
c = [0] * q
d = [0] * q
ans = 0
for i in range(q):
    a[i], b[i], c[i], d[i] = list(map(int, input().split()))
dfs(tuple([1]))
print(ans)

-from itertools import combinations_with_replacement +def dfs(A): +    global ans +    if len(A) == n + 1: +        now = 0 +        for j in range(q): +            if A[b[j]] - A[a[j]] == c[j]: +                now += d[j] +        ans = max(ans, now) +        return +    B = list(A) +    B.append(A[-1]) +    while B[-1] <= m: +        dfs(tuple(B)) +        B[-1] += 1 + -for seq in combinations_with_replacement([i for i in range(1, m + 1)], n): -    now = 0 -    for j in range(q): -        if seq[b[j] - 1] - seq[a[j] - 1] == c[j]: -            now += d[j] -    ans = max(ans, now) +dfs(tuple([1]))