import itertools

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

a,b,c,d = [],[],[],[]

for i in range(q):

	A,B,C,D = list(map(int, input().split()))

	a.append(A)

	b.append(B)

	c.append(C)

	d.append(D)

e = [i+1 for i in range(m)]

ans = 0

for i in itertools.combinations_with_replacement(e,n-1):

	f = list(i)

	f.append(1)

	f.sort()

	w = 0

	for j in range(q):

		if f[b[j]-1]-f[a[j]-1] == c[j]:

			w += d[j]

	ans = max(ans,w)

print (ans)

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

query = []

for i in range(q):

	query.append(list(map(int, input().split())))



v = []

def dfs(u,ary):

	if len(ary) == n:

		ary.sort()

		cnt = 0

		for j in range(q):

			a,b,c,d = query[j]

			if ary[b-1] - ary[a-1] == c:

				cnt += d

		v.append(cnt)

	else:

		for j in range(u,m+1):

			dfs(j,ary+[j])





dfs(1,[1])



print((max(v)))
import itertools

n, m, q = list(map(int, input().split()))
a, b, c, d = [], [], [], []
for i in range(q):
    A, B, C, D = list(map(int, input().split()))
    a.append(A)
    b.append(B)
    c.append(C)
    d.append(D)
e = [i + 1 for i in range(m)]
ans = 0
for i in itertools.combinations_with_replacement(e, n - 1):
    f = list(i)
    f.append(1)
    f.sort()
    w = 0
    for j in range(q):
        if f[b[j] - 1] - f[a[j] - 1] == c[j]:
            w += d[j]
    ans = max(ans, w)
print(ans)

n, m, q = list(map(int, input().split()))
query = []
for i in range(q):
    query.append(list(map(int, input().split())))
v = []


def dfs(u, ary):
    if len(ary) == n:
        ary.sort()
        cnt = 0
        for j in range(q):
            a, b, c, d = query[j]
            if ary[b - 1] - ary[a - 1] == c:
                cnt += d
        v.append(cnt)
    else:
        for j in range(u, m + 1):
            dfs(j, ary + [j])


dfs(1, [1])
print((max(v)))

-import itertools +n, m, q = list(map(int, input().split())) +query = [] +for i in range(q): +    query.append(list(map(int, input().split()))) +v = [] -n, m, q = list(map(int, input().split())) -a, b, c, d = [], [], [], [] -for i in range(q): -    A, B, C, D = list(map(int, input().split())) -    a.append(A) -    b.append(B) -    c.append(C) -    d.append(D) -e = [i + 1 for i in range(m)] -ans = 0 -for i in itertools.combinations_with_replacement(e, n - 1): -    f = list(i) -    f.append(1) -    f.sort() -    w = 0 -    for j in range(q): -        if f[b[j] - 1] - f[a[j] - 1] == c[j]: -            w += d[j] -    ans = max(ans, w) -print(ans) + +def dfs(u, ary): +    if len(ary) == n: +        ary.sort() +        cnt = 0 +        for j in range(q): +            a, b, c, d = query[j] +            if ary[b - 1] - ary[a - 1] == c: +                cnt += d +        v.append(cnt) +    else: +        for j in range(u, m + 1): +            dfs(j, ary + [j]) + + +dfs(1, [1]) +print((max(v)))