from itertools import combinations_with_replacement as comb

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

ans = 0

query = [[int(i) for i in input().split()] for _ in range(q)]

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

    cnt = 0

    for i in range(q):

        a,b,c,d = query[i]

        if A[b-1]-A[a-1] == c: cnt += d

    ans = max(cnt,ans)

print(ans)

import sys

sys.setrecursionlimit(10**9)

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

query = [[int(i) for i in input().split()] for _ in range(q)]



def f(A):

    if len(A) == n+1:

        # print(A)

        cnt = 0

        for i in range(q):

            a,b,c,d = query[i]

            if A[b]-A[a] == c: cnt += d

        return cnt

    res = 0

    for i in range(A[-1],m+1):

        res = max(res,f(A+[i]))

    return res



print((f([1])))
from itertools import combinations_with_replacement as comb

n, m, q = list(map(int, input().split()))
ans = 0
query = [[int(i) for i in input().split()] for _ in range(q)]
for A in comb(list(range(1, m + 1)), n):
    cnt = 0
    for i in range(q):
        a, b, c, d = query[i]
        if A[b - 1] - A[a - 1] == c:
            cnt += d
    ans = max(cnt, ans)
print(ans)

import sys

sys.setrecursionlimit(10**9)
n, m, q = list(map(int, input().split()))
query = [[int(i) for i in input().split()] for _ in range(q)]


def f(A):
    if len(A) == n + 1:
        # print(A)
        cnt = 0
        for i in range(q):
            a, b, c, d = query[i]
            if A[b] - A[a] == c:
                cnt += d
        return cnt
    res = 0
    for i in range(A[-1], m + 1):
        res = max(res, f(A + [i]))
    return res


print((f([1])))

-from itertools import combinations_with_replacement as comb +import sys +sys.setrecursionlimit(10**9) -ans = 0 -for A in comb(list(range(1, m + 1)), n): -    cnt = 0 -    for i in range(q): -        a, b, c, d = query[i] -        if A[b - 1] - A[a - 1] == c: -            cnt += d -    ans = max(cnt, ans) -print(ans) + + +def f(A): +    if len(A) == n + 1: +        # print(A) +        cnt = 0 +        for i in range(q): +            a, b, c, d = query[i] +            if A[b] - A[a] == c: +                cnt += d +        return cnt +    res = 0 +    for i in range(A[-1], m + 1): +        res = max(res, f(A + [i])) +    return res + + +print((f([1])))