import itertools

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

lis=[]

ans=0



for _ in range(q):

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

    lis.append([a-1,b-1,c,d])



for i in itertools.combinations_with_replacement(list(range(1,m+1)),n):

    tmp=0

    for j in range(q):

        if i[lis[j][1]] - i[lis[j][0]] == lis[j][2]:

            tmp+=lis[j][3]

    ans=max(ans,tmp)

print(ans)
import itertools

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

a=[0 for i in range(q)]

b=[0 for i in range(q)]

c=[0 for i in range(q)]

d=[0 for i in range(q)]

for i in range(q):

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

    a[i]-=1

    b[i]-=1

ans=0



for i in itertools.combinations_with_replacement(list(range(1,m+1)),n):

    tmp=0

    for j in range(q):

        if i[b[j]] - i[a[j]] == c[j]:

            tmp+=d[j]

    ans=max(ans,tmp)

print(ans)
import itertools

n, m, q = list(map(int, input().split()))
lis = []
ans = 0
for _ in range(q):
    a, b, c, d = list(map(int, input().split()))
    lis.append([a - 1, b - 1, c, d])
for i in itertools.combinations_with_replacement(list(range(1, m + 1)), n):
    tmp = 0
    for j in range(q):
        if i[lis[j][1]] - i[lis[j][0]] == lis[j][2]:
            tmp += lis[j][3]
    ans = max(ans, tmp)
print(ans)

import itertools

n, m, q = list(map(int, input().split()))
a = [0 for i in range(q)]
b = [0 for i in range(q)]
c = [0 for i in range(q)]
d = [0 for i in range(q)]
for i in range(q):
    a[i], b[i], c[i], d[i] = list(map(int, input().split()))
    a[i] -= 1
    b[i] -= 1
ans = 0
for i in itertools.combinations_with_replacement(list(range(1, m + 1)), n):
    tmp = 0
    for j in range(q):
        if i[b[j]] - i[a[j]] == c[j]:
            tmp += d[j]
    ans = max(ans, tmp)
print(ans)

-lis = [] +a = [0 for i in range(q)] +b = [0 for i in range(q)] +c = [0 for i in range(q)] +d = [0 for i in range(q)] +for i in range(q): +    a[i], b[i], c[i], d[i] = list(map(int, input().split())) +    a[i] -= 1 +    b[i] -= 1 -for _ in range(q): -    a, b, c, d = list(map(int, input().split())) -    lis.append([a - 1, b - 1, c, d]) -        if i[lis[j][1]] - i[lis[j][0]] == lis[j][2]: -            tmp += lis[j][3] +        if i[b[j]] - i[a[j]] == c[j]: +            tmp += d[j]