f=lambda:list(map(int,input().split()))

n,m,q=f()

lq=[tuple(f()) for _ in range(q)]

ans=0

from itertools import *

for t in combinations_with_replacement(list(range(m)),n):

  s=0

  for a,b,c,d in lq:

    if t[b-1]-t[a-1]==c: s+=d

  ans=max(ans,s)

print(ans)
f=lambda:list(map(int,input().split()))

n,m,q=f()

Q=[list(f())for _ in range(q)]

from itertools import*

print((max(sum(d for a,b,c,d in Q if l[b-1]-l[a-1]==c)for l in combinations_with_replacement(list(range(m)),n))))
f = lambda: list(map(int, input().split()))
n, m, q = f()
lq = [tuple(f()) for _ in range(q)]
ans = 0
from itertools import *

for t in combinations_with_replacement(list(range(m)), n):
    s = 0
    for a, b, c, d in lq:
        if t[b - 1] - t[a - 1] == c:
            s += d
    ans = max(ans, s)
print(ans)

f = lambda: list(map(int, input().split()))
n, m, q = f()
Q = [list(f()) for _ in range(q)]
from itertools import *

print(
    (
        max(
            sum(d for a, b, c, d in Q if l[b - 1] - l[a - 1] == c)
            for l in combinations_with_replacement(list(range(m)), n)
        )
    )
)

-lq = [tuple(f()) for _ in range(q)] -ans = 0 +Q = [list(f()) for _ in range(q)] -for t in combinations_with_replacement(list(range(m)), n): -    s = 0 -    for a, b, c, d in lq: -        if t[b - 1] - t[a - 1] == c: -            s += d -    ans = max(ans, s) -print(ans) +print( +    ( +        max( +            sum(d for a, b, c, d in Q if l[b - 1] - l[a - 1] == c) +            for l in combinations_with_replacement(list(range(m)), n) +        ) +    ) +)