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

A, B, C, D = [], [], [], []

for i in range(q):

    a, b, c, d = list(map(int, input().split()))

    A.append(a - 1)

    B.append(b - 1)

    C.append(c)

    D.append(d)



L = []



def f(l):

    if len(l) == n:

        L.append(l)

        return

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

        f(l + [i])



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

    f([i])



ans = 0



for l in L:

    ans = max(ans, sum(d for a, b, c, d in zip(A, B, C, D) if l[b] - l[a] == c))



print(ans)

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

T = []

for i in range(q):

    a, b, c, d = list(map(int, input().split()))

    T.append((a - 1, b - 1, c, d))



L = []

def f(l):

    if len(l) == n + 1:

        L.append(l[1:])

        return

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

        f(l + [i])

f([1])



print((max(sum(d for a, b, c, d in T if l[b] - l[a] == c) for l in L)))

n, m, q = list(map(int, input().split()))
A, B, C, D = [], [], [], []
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    A.append(a - 1)
    B.append(b - 1)
    C.append(c)
    D.append(d)
L = []


def f(l):
    if len(l) == n:
        L.append(l)
        return
    for i in range(l[-1], m + 1):
        f(l + [i])


for i in range(1, m + 1):
    f([i])
ans = 0
for l in L:
    ans = max(ans, sum(d for a, b, c, d in zip(A, B, C, D) if l[b] - l[a] == c))
print(ans)

n, m, q = list(map(int, input().split()))
T = []
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    T.append((a - 1, b - 1, c, d))
L = []


def f(l):
    if len(l) == n + 1:
        L.append(l[1:])
        return
    for i in range(l[-1], m + 1):
        f(l + [i])


f([1])
print((max(sum(d for a, b, c, d in T if l[b] - l[a] == c) for l in L)))

-A, B, C, D = [], [], [], [] +T = [] -    A.append(a - 1) -    B.append(b - 1) -    C.append(c) -    D.append(d) +    T.append((a - 1, b - 1, c, d)) -    if len(l) == n: -        L.append(l) +    if len(l) == n + 1: +        L.append(l[1:]) -for i in range(1, m + 1): -    f([i]) -ans = 0 -for l in L: -    ans = max(ans, sum(d for a, b, c, d in zip(A, B, C, D) if l[b] - l[a] == c)) -print(ans) +f([1]) +print((max(sum(d for a, b, c, d in T if l[b] - l[a] == c) for l in L)))