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

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

from itertools import *

ans = 0

for A in combinations_with_replacement(list(range(1, m+1)), n): # nHr

    now = sum(d if A[b-1] - A[a-1] == c else 0 for a,b,c,d in abcd)

    ans = max(ans, now)

print(ans)

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

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



def dfs(A=[1]): # A:1 origin

    if n+1 <= len(A):

        return sum(d if A[b]-A[a]==c else 0 for a,b,c,d in abcd)

    A.append(A[-1])

    ret = 0

    while A[-1] <= m:

        ret = max(ret, dfs(A))

        A[-1] += 1

    A.pop()

    return ret



ans = dfs()

print(ans)

n, m, q = list(map(int, input().split()))
abcd = [[*list(map(int, input().split()))] for _ in range(q)]
from itertools import *

ans = 0
for A in combinations_with_replacement(list(range(1, m + 1)), n):  # nHr
    now = sum(d if A[b - 1] - A[a - 1] == c else 0 for a, b, c, d in abcd)
    ans = max(ans, now)
print(ans)

n, m, q = list(map(int, input().split()))
abcd = [[*list(map(int, input().split()))] for _ in range(q)]


def dfs(A=[1]):  # A:1 origin
    if n + 1 <= len(A):
        return sum(d if A[b] - A[a] == c else 0 for a, b, c, d in abcd)
    A.append(A[-1])
    ret = 0
    while A[-1] <= m:
        ret = max(ret, dfs(A))
        A[-1] += 1
    A.pop()
    return ret


ans = dfs()
print(ans)

-from itertools import * -ans = 0 -for A in combinations_with_replacement(list(range(1, m + 1)), n):  # nHr -    now = sum(d if A[b - 1] - A[a - 1] == c else 0 for a, b, c, d in abcd) -    ans = max(ans, now) + +def dfs(A=[1]):  # A:1 origin +    if n + 1 <= len(A): +        return sum(d if A[b] - A[a] == c else 0 for a, b, c, d in abcd) +    A.append(A[-1]) +    ret = 0 +    while A[-1] <= m: +        ret = max(ret, dfs(A)) +        A[-1] += 1 +    A.pop() +    return ret + + +ans = dfs()