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

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



ans = 0

import itertools

for i in itertools.combinations_with_replacement(list(range(1, m+1)), n):

    sumd = 0

    for j in abcd:

        if i[j[1]-1] - i[j[0]-1] == j[2]:

            sumd += j[3]

    ans = max(ans, sumd)

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

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



import sys

sys.setrecursionlimit(10**6)

ans = [0]

def dfs(idx, now):

    if idx == n:

        #print(now)

        sumd = 0

        for j in abcd:

            if now[j[1]-1] - now[j[0]-1] == j[2]:

                sumd += j[3]

        ans[0] = max(ans[0], sumd)

        return

    if len(now) > 0:

        start = now[-1]

    else:

        start = 1

    for i in range(start, m+1):

        now.append(i)

        dfs(idx + 1, now)

        now.pop()



dfs(0, [])

print((ans[0]))
n, m, q = list(map(int, input().split()))
abcd = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0
import itertools

for i in itertools.combinations_with_replacement(list(range(1, m + 1)), n):
    sumd = 0
    for j in abcd:
        if i[j[1] - 1] - i[j[0] - 1] == j[2]:
            sumd += j[3]
    ans = max(ans, sumd)
print(ans)

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

sys.setrecursionlimit(10**6)
ans = [0]


def dfs(idx, now):
    if idx == n:
        # print(now)
        sumd = 0
        for j in abcd:
            if now[j[1] - 1] - now[j[0] - 1] == j[2]:
                sumd += j[3]
        ans[0] = max(ans[0], sumd)
        return
    if len(now) > 0:
        start = now[-1]
    else:
        start = 1
    for i in range(start, m + 1):
        now.append(i)
        dfs(idx + 1, now)
        now.pop()


dfs(0, [])
print((ans[0]))

-ans = 0 -import itertools +import sys -for i in itertools.combinations_with_replacement(list(range(1, m + 1)), n): -    sumd = 0 -    for j in abcd: -        if i[j[1] - 1] - i[j[0] - 1] == j[2]: -            sumd += j[3] -    ans = max(ans, sumd) -print(ans) +sys.setrecursionlimit(10**6) +ans = [0] + + +def dfs(idx, now): +    if idx == n: +        # print(now) +        sumd = 0 +        for j in abcd: +            if now[j[1] - 1] - now[j[0] - 1] == j[2]: +                sumd += j[3] +        ans[0] = max(ans[0], sumd) +        return +    if len(now) > 0: +        start = now[-1] +    else: +        start = 1 +    for i in range(start, m + 1): +        now.append(i) +        dfs(idx + 1, now) +        now.pop() + + +dfs(0, []) +print((ans[0]))