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

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

ans = 0



def calc_score(p):

    score = 0

    for a, b, c, d in queries:

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

            score += d

    return score



def f(p):

    if len(p) == n:

        global ans

        ans = max(ans, calc_score(p))

        return

    l = p[-1] if len(p) else 1

    for x in range(l, m+1):

        f(p + [x])



f([])



print(ans)

from itertools import combinations_with_replacement as comb



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

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

ans = 0



def calc_score(p):

    score = 0

    for a, b, c, d in queries:

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

            score += d

    return score



for p in comb(list(range(1, m+1)), n):

    ans = max(ans, calc_score(p))



print(ans)

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


def calc_score(p):
    score = 0
    for a, b, c, d in queries:
        if p[b - 1] - p[a - 1] == c:
            score += d
    return score


def f(p):
    if len(p) == n:
        global ans
        ans = max(ans, calc_score(p))
        return
    l = p[-1] if len(p) else 1
    for x in range(l, m + 1):
        f(p + [x])


f([])
print(ans)

from itertools import combinations_with_replacement as comb

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


def calc_score(p):
    score = 0
    for a, b, c, d in queries:
        if p[b - 1] - p[a - 1] == c:
            score += d
    return score


for p in comb(list(range(1, m + 1)), n):
    ans = max(ans, calc_score(p))
print(ans)

+from itertools import combinations_with_replacement as comb + -def f(p): -    if len(p) == n: -        global ans -        ans = max(ans, calc_score(p)) -        return -    l = p[-1] if len(p) else 1 -    for x in range(l, m + 1): -        f(p + [x]) - - -f([]) +for p in comb(list(range(1, m + 1)), n): +    ans = max(ans, calc_score(p))