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

abcd=[]

for _ in range(q):

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

	abcd.append((a,b,c,d))

ans=0

import itertools

p=itertools.combinations_with_replacement(list(range(1,m+1)),n)

for x in p:

	r=0

	for i in range(q):

		if x[abcd[i][1]-1]-x[abcd[i][0]-1]==abcd[i][2]:

			r+=abcd[i][3]

	ans=max(ans,r)

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

abcd=[]

for _ in range(q):

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

	abcd.append((a,b,c,d))

ans=0

p=[]

import sys

sys.setrecursionlimit(10**6)

def dfs(x):

	if len(x)==n:

		p.append(x)

	else:

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

			dfs(x+[x[-1]+i])

dfs([1])

# import itertools

# p=itertools.combinations_with_replacement(range(1,m+1),n)

for x in p:

	r=0

	for i in range(q):

		if x[abcd[i][1]-1]-x[abcd[i][0]-1]==abcd[i][2]:

			r+=abcd[i][3]

	ans=max(ans,r)

print(ans)
n, m, q = list(map(int, input().split()))
abcd = []
for _ in range(q):
    a, b, c, d = list(map(int, input().split()))
    abcd.append((a, b, c, d))
ans = 0
import itertools

p = itertools.combinations_with_replacement(list(range(1, m + 1)), n)
for x in p:
    r = 0
    for i in range(q):
        if x[abcd[i][1] - 1] - x[abcd[i][0] - 1] == abcd[i][2]:
            r += abcd[i][3]
    ans = max(ans, r)
print(ans)

n, m, q = list(map(int, input().split()))
abcd = []
for _ in range(q):
    a, b, c, d = list(map(int, input().split()))
    abcd.append((a, b, c, d))
ans = 0
p = []
import sys

sys.setrecursionlimit(10**6)


def dfs(x):
    if len(x) == n:
        p.append(x)
    else:
        for i in range(m - x[-1] + 1):
            dfs(x + [x[-1] + i])


dfs([1])
# import itertools
# p=itertools.combinations_with_replacement(range(1,m+1),n)
for x in p:
    r = 0
    for i in range(q):
        if x[abcd[i][1] - 1] - x[abcd[i][0] - 1] == abcd[i][2]:
            r += abcd[i][3]
    ans = max(ans, r)
print(ans)

-import itertools +p = [] +import sys -p = itertools.combinations_with_replacement(list(range(1, m + 1)), n) +sys.setrecursionlimit(10**6) + + +def dfs(x): +    if len(x) == n: +        p.append(x) +    else: +        for i in range(m - x[-1] + 1): +            dfs(x + [x[-1] + i]) + + +dfs([1]) +# import itertools +# p=itertools.combinations_with_replacement(range(1,m+1),n)