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

a=[0]*Q

b=[0]*Q

c=[0]*Q

d=[0]*Q

for i in range(Q):

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

import itertools

ans=0

for A in itertools.combinations_with_replacement(list(range(1,M+1)),N):

  A_=list(A)

  s=0

  

  for i in range(Q):

    if A_[b[i]-1]-A_[a[i]-1]==c[i]:

      s+=d[i]

    ans=max(ans,s)

print(ans)  
def dfs(seq):

    ans = 0

    if len(seq) == n:

        kou = 0

        for u in data:

            if seq[u[1]-1] - seq[u[0]-1] == u[2]:

                kou+=u[3]

        return kou

    else:#len(seq)==N-1からの遷都を考えると良い

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

            seq_next=seq+[i]

            ans = max(ans, dfs(seq_next))

        return ans



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

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



score = dfs([1])

print(score)

N, M, Q = list(map(int, input().split()))
a = [0] * Q
b = [0] * Q
c = [0] * Q
d = [0] * Q
for i in range(Q):
    a[i], b[i], c[i], d[i] = list(map(int, input().split()))
import itertools

ans = 0
for A in itertools.combinations_with_replacement(list(range(1, M + 1)), N):
    A_ = list(A)
    s = 0
    for i in range(Q):
        if A_[b[i] - 1] - A_[a[i] - 1] == c[i]:
            s += d[i]
        ans = max(ans, s)
print(ans)

def dfs(seq):
    ans = 0
    if len(seq) == n:
        kou = 0
        for u in data:
            if seq[u[1] - 1] - seq[u[0] - 1] == u[2]:
                kou += u[3]
        return kou
    else:  # len(seq)==N-1からの遷都を考えると良い
        for i in range(seq[-1], m + 1):
            seq_next = seq + [i]
            ans = max(ans, dfs(seq_next))
        return ans


n, m, q = list(map(int, input().split()))
data = [list(map(int, input().split())) for _ in range(q)]
score = dfs([1])
print(score)

-N, M, Q = list(map(int, input().split())) -a = [0] * Q -b = [0] * Q -c = [0] * Q -d = [0] * Q -for i in range(Q): -    a[i], b[i], c[i], d[i] = list(map(int, input().split())) -import itertools +def dfs(seq): +    ans = 0 +    if len(seq) == n: +        kou = 0 +        for u in data: +            if seq[u[1] - 1] - seq[u[0] - 1] == u[2]: +                kou += u[3] +        return kou +    else:  # len(seq)==N-1からの遷都を考えると良い +        for i in range(seq[-1], m + 1): +            seq_next = seq + [i] +            ans = max(ans, dfs(seq_next)) +        return ans -ans = 0 -for A in itertools.combinations_with_replacement(list(range(1, M + 1)), N): -    A_ = list(A) -    s = 0 -    for i in range(Q): -        if A_[b[i] - 1] - A_[a[i] - 1] == c[i]: -            s += d[i] -        ans = max(ans, s) -print(ans) + +n, m, q = list(map(int, input().split())) +data = [list(map(int, input().split())) for _ in range(q)] +score = dfs([1]) +print(score)