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

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

ls = []





def dfs(x, s):

    if len(s) == n:

        ls.append(s)

        return 0

    else:

        for y in range(x, m):

            dfs(y, s+str(y))





dfs(0, "")

ans = 0

for x in ls:

    x = tuple([int(x)+1 for x in list(x)])

    cnt = 0

    for a, b, c, d in abcd:

        a -= 1

        b -= 1

        if x[b]-x[a] == c:

            cnt += d

    if ans < cnt:

        ans = cnt

print(ans)
from itertools import combinations_with_replacement

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

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

ans = 0

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

    cnt = 0

    for a, b, c, d in abcd:

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

            cnt += d

    if ans < cnt:

        ans = cnt



print(ans)

n, m, q = list(map(int, input().split()))
abcd = [tuple(map(int, input().split())) for _ in range(q)]
ls = []


def dfs(x, s):
    if len(s) == n:
        ls.append(s)
        return 0
    else:
        for y in range(x, m):
            dfs(y, s + str(y))


dfs(0, "")
ans = 0
for x in ls:
    x = tuple([int(x) + 1 for x in list(x)])
    cnt = 0
    for a, b, c, d in abcd:
        a -= 1
        b -= 1
        if x[b] - x[a] == c:
            cnt += d
    if ans < cnt:
        ans = cnt
print(ans)

from itertools import combinations_with_replacement

n, m, q = list(map(int, input().split()))
abcd = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0
for x in combinations_with_replacement(list(range(1, m + 1)), n):
    cnt = 0
    for a, b, c, d in abcd:
        if x[b - 1] - x[a - 1] == c:
            cnt += d
    if ans < cnt:
        ans = cnt
print(ans)

+from itertools import combinations_with_replacement + -ls = [] - - -def dfs(x, s): -    if len(s) == n: -        ls.append(s) -        return 0 -    else: -        for y in range(x, m): -            dfs(y, s + str(y)) - - -dfs(0, "") -for x in ls: -    x = tuple([int(x) + 1 for x in list(x)]) +for x in combinations_with_replacement(list(range(1, m + 1)), n): -        a -= 1 -        b -= 1 -        if x[b] - x[a] == c: +        if x[b - 1] - x[a - 1] == c: