import itertools



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()))

    

A=list(itertools.combinations_with_replacement([i for i in range(1,m+1)],n))





g=[]

for v in A:

    e=0

    for i in range(q):

        

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

            e += d[i]

            

        

    g += [e]

print((max(g)))
from itertools import combinations_with_replacement



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

summa=[]

res=[]

for i in range(q):

    a=[int(i) for i in input().split()]

    summa.append(a)

    

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

    cnt=0

    for j in summa:

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

                cnt += j[3]

            

    res.append(cnt)

    

print((max(res)))
import itertools

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()))
A = list(itertools.combinations_with_replacement([i for i in range(1, m + 1)], n))
g = []
for v in A:
    e = 0
    for i in range(q):
        if v[b[i] - 1] - v[a[i] - 1] == c[i]:
            e += d[i]
    g += [e]
print((max(g)))

from itertools import combinations_with_replacement

n, m, q = list(map(int, input().split()))
summa = []
res = []
for i in range(q):
    a = [int(i) for i in input().split()]
    summa.append(a)
for i in combinations_with_replacement(list(range(1, m + 1)), n):
    cnt = 0
    for j in summa:
        if i[j[1] - 1] - i[j[0] - 1] == j[2]:
            cnt += j[3]
    res.append(cnt)
print((max(res)))

-import itertools +from itertools import combinations_with_replacement -a = [0] * q -b = [0] * q -c = [0] * q -d = [0] * q +summa = [] +res = [] -    a[i], b[i], c[i], d[i] = list(map(int, input().split())) -A = list(itertools.combinations_with_replacement([i for i in range(1, m + 1)], n)) -g = [] -for v in A: -    e = 0 -    for i in range(q): -        if v[b[i] - 1] - v[a[i] - 1] == c[i]: -            e += d[i] -    g += [e] -print((max(g))) +    a = [int(i) for i in input().split()] +    summa.append(a) +for i in combinations_with_replacement(list(range(1, m + 1)), n): +    cnt = 0 +    for j in summa: +        if i[j[1] - 1] - i[j[0] - 1] == j[2]: +            cnt += j[3] +    res.append(cnt) +print((max(res)))