import sys

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

read = sys.stdin.buffer.read

sys.setrecursionlimit(10 ** 7)



N, M, Q = list(map(int, readline().split()))

q_kumi = []

for _ in range(Q):

    q_kumi.append(list(map(int, readline().split())))



import itertools

AL = itertools.combinations_with_replacement(list(range(1, M+1)), N)



ans = 0

for A in AL:

    tmp_ans = 0

    for kumi in q_kumi:

        if A[kumi[1]-1] - A[kumi[0]-1] == kumi[2]:

            tmp_ans += kumi[3]

    ans = max(ans, tmp_ans)





print(ans)
import sys



readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

read = sys.stdin.buffer.read

sys.setrecursionlimit(10 ** 7)

INF = float('inf')



N, M, Q = list(map(int, readline().split()))



kumi = []

for _ in range(Q):

    tmp = list(map(int, readline().split()))

    kumi.append(tmp)



ans = 0

res = []

def F(l):

    if len(l) == N:

        ans = 0

        for i in range(Q):

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

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

                ans += d

        yield ans

        return

    start = l[-1] if len(l) else 0

    for i in range(start, M):

        l.append(i)

        yield from F(l)

        l.pop()

    

ans = max(F([]))

print(ans)
import sys

readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10**7)
N, M, Q = list(map(int, readline().split()))
q_kumi = []
for _ in range(Q):
    q_kumi.append(list(map(int, readline().split())))
import itertools

AL = itertools.combinations_with_replacement(list(range(1, M + 1)), N)
ans = 0
for A in AL:
    tmp_ans = 0
    for kumi in q_kumi:
        if A[kumi[1] - 1] - A[kumi[0] - 1] == kumi[2]:
            tmp_ans += kumi[3]
    ans = max(ans, tmp_ans)
print(ans)

import sys

readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10**7)
INF = float("inf")
N, M, Q = list(map(int, readline().split()))
kumi = []
for _ in range(Q):
    tmp = list(map(int, readline().split()))
    kumi.append(tmp)
ans = 0
res = []


def F(l):
    if len(l) == N:
        ans = 0
        for i in range(Q):
            a, b, c, d = kumi[i]
            if l[b - 1] - l[a - 1] == c:
                ans += d
        yield ans
        return
    start = l[-1] if len(l) else 0
    for i in range(start, M):
        l.append(i)
        yield from F(l)
        l.pop()


ans = max(F([]))
print(ans)

+INF = float("inf") -q_kumi = [] +kumi = [] -    q_kumi.append(list(map(int, readline().split()))) -import itertools +    tmp = list(map(int, readline().split())) +    kumi.append(tmp) +ans = 0 +res = [] -AL = itertools.combinations_with_replacement(list(range(1, M + 1)), N) -ans = 0 -for A in AL: -    tmp_ans = 0 -    for kumi in q_kumi: -        if A[kumi[1] - 1] - A[kumi[0] - 1] == kumi[2]: -            tmp_ans += kumi[3] -    ans = max(ans, tmp_ans) + +def F(l): +    if len(l) == N: +        ans = 0 +        for i in range(Q): +            a, b, c, d = kumi[i] +            if l[b - 1] - l[a - 1] == c: +                ans += d +        yield ans +        return +    start = l[-1] if len(l) else 0 +    for i in range(start, M): +        l.append(i) +        yield from F(l) +        l.pop() + + +ans = max(F([]))