from itertools import combinations_with_replacement

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

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

s=0

for p in combinations_with_replacement(list(range(1,m+1)),n):

    t=0

    for a,b,c,d in abcd:

        t+=d*(p[b-1]-p[a-1]==c)

    s=max(s,t)

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

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

ans,now=0,0

def chk(x):

    global ans

    if len(x)>=n+1:

        now=0

        for a,b,c,d in abcd:

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

                now+=d

        ans=max(ans,now)

        return

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

        chk(x+[i])

chk([1])

print(ans)
from itertools import combinations_with_replacement

n, m, q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for _ in range(q)]
s = 0
for p in combinations_with_replacement(list(range(1, m + 1)), n):
    t = 0
    for a, b, c, d in abcd:
        t += d * (p[b - 1] - p[a - 1] == c)
    s = max(s, t)
print(s)

n, m, q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for _ in range(q)]
ans, now = 0, 0


def chk(x):
    global ans
    if len(x) >= n + 1:
        now = 0
        for a, b, c, d in abcd:
            if x[b - 1] - x[a - 1] == c:
                now += d
        ans = max(ans, now)
        return
    for i in range(x[-1], m + 1):
        chk(x + [i])


chk([1])
print(ans)

-from itertools import combinations_with_replacement - -s = 0 -for p in combinations_with_replacement(list(range(1, m + 1)), n): -    t = 0 -    for a, b, c, d in abcd: -        t += d * (p[b - 1] - p[a - 1] == c) -    s = max(s, t) -print(s) +ans, now = 0, 0 + + +def chk(x): +    global ans +    if len(x) >= n + 1: +        now = 0 +        for a, b, c, d in abcd: +            if x[b - 1] - x[a - 1] == c: +                now += d +        ans = max(ans, now) +        return +    for i in range(x[-1], m + 1): +        chk(x + [i]) + + +chk([1]) +print(ans)