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

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

arr = []



def f(p):

    if len(p) == n+1:

        arr.append(p[:-1])

        return

    while p[-1] <= m:

        f(p + [p[-1]])

        p[-1] += 1



f([1])



ans = 0

for p in arr:

    score = 0

    for a, b, c, d in queries:

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

            score += d

    ans = max(ans, score)



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+1:

        global ans

        ans = max(ans, calc_score(p))

        return

    while p[-1] <= m:

        f(p + [p[-1]])

        p[-1] += 1



f([1])



print(ans)

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


def f(p):
    if len(p) == n + 1:
        arr.append(p[:-1])
        return
    while p[-1] <= m:
        f(p + [p[-1]])
        p[-1] += 1


f([1])
ans = 0
for p in arr:
    score = 0
    for a, b, c, d in queries:
        if p[b - 1] - p[a - 1] == c:
            score += d
    ans = max(ans, score)
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 + 1:
        global ans
        ans = max(ans, calc_score(p))
        return
    while p[-1] <= m:
        f(p + [p[-1]])
        p[-1] += 1


f([1])
print(ans)

-arr = [] +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 -        arr.append(p[:-1]) +        global ans +        ans = max(ans, calc_score(p)) -ans = 0 -for p in arr: -    score = 0 -    for a, b, c, d in queries: -        if p[b - 1] - p[a - 1] == c: -            score += d -    ans = max(ans, score)