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

abcd = [list(map(int,input().split())) for i in range(Q)]

ans = 0

for a in range(1,M+1):

    for b in range(a,M+1):

        for c in range(b,M+1):

            for d in range(c,M+1):

                for e in range(d,M+1):

                    for f in range(e,M+1):

                        for g in range(f,M+1):

                            for h in range(g,M+1):

                                for i in range(h,M+1):

                                    for j in range(i,M+1):

                                        lista = [a,b,c,d,e,f,g,h,i,j]

                                        ans1 = 0

                                        for A,B,C,D in abcd:

                                            if lista[B-1]-lista[A-1] == C:

                                                ans1 += D

                                        ans = max(ans,ans1)

print(ans)

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

abcd = [list(map(int,input().split())) for i in range(Q)]

ans = 0

def dfs(n,A,now):

    global ans

    if n == 0:

        ansi = 0

        for a,b,c,d in abcd:

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

                ansi+=d

        ans = max(ans,ansi)

    else:

        for i in range(now,M+1):

            dfs(n-1,A+[i],i)



A = []

dfs(N,A,1)

print(ans)
N, M, Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for i in range(Q)]
ans = 0
for a in range(1, M + 1):
    for b in range(a, M + 1):
        for c in range(b, M + 1):
            for d in range(c, M + 1):
                for e in range(d, M + 1):
                    for f in range(e, M + 1):
                        for g in range(f, M + 1):
                            for h in range(g, M + 1):
                                for i in range(h, M + 1):
                                    for j in range(i, M + 1):
                                        lista = [a, b, c, d, e, f, g, h, i, j]
                                        ans1 = 0
                                        for A, B, C, D in abcd:
                                            if lista[B - 1] - lista[A - 1] == C:
                                                ans1 += D
                                        ans = max(ans, ans1)
print(ans)

N, M, Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for i in range(Q)]
ans = 0


def dfs(n, A, now):
    global ans
    if n == 0:
        ansi = 0
        for a, b, c, d in abcd:
            if A[b - 1] - A[a - 1] == c:
                ansi += d
        ans = max(ans, ansi)
    else:
        for i in range(now, M + 1):
            dfs(n - 1, A + [i], i)


A = []
dfs(N, A, 1)
print(ans)

-for a in range(1, M + 1): -    for b in range(a, M + 1): -        for c in range(b, M + 1): -            for d in range(c, M + 1): -                for e in range(d, M + 1): -                    for f in range(e, M + 1): -                        for g in range(f, M + 1): -                            for h in range(g, M + 1): -                                for i in range(h, M + 1): -                                    for j in range(i, M + 1): -                                        lista = [a, b, c, d, e, f, g, h, i, j] -                                        ans1 = 0 -                                        for A, B, C, D in abcd: -                                            if lista[B - 1] - lista[A - 1] == C: -                                                ans1 += D -                                        ans = max(ans, ans1) + + +def dfs(n, A, now): +    global ans +    if n == 0: +        ansi = 0 +        for a, b, c, d in abcd: +            if A[b - 1] - A[a - 1] == c: +                ansi += d +        ans = max(ans, ansi) +    else: +        for i in range(now, M + 1): +            dfs(n - 1, A + [i], i) + + +A = [] +dfs(N, A, 1)