# C - Many Requirements



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

lst = []

for i in range(q):

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

    lst.append((a, b, c, d))  # 得点を先頭に



def allseq(n, m):

    if n == 0:

        yield []

    else:

        for rest in allseq(n - 1, m):

            for i in range(rest[-1] if len(rest) > 0 else 1, m+1):

                yield rest + [i]



x = 0

for seq in allseq(n, m):

    s = 0

    for a, b, c, d in lst:

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

            s += d

    x = max(x, s)



print(x)

# C - Many Requirements



from itertools import combinations_with_replacement



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

lst = []

for i in range(q):

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

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



x = max(sum(d for a, b, c, d in lst if seq[b-1] - seq[a-1] == c)

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

print(x)

# C - Many Requirements
n, m, q = list(map(int, input().split()))
lst = []
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    lst.append((a, b, c, d))  # 得点を先頭に


def allseq(n, m):
    if n == 0:
        yield []
    else:
        for rest in allseq(n - 1, m):
            for i in range(rest[-1] if len(rest) > 0 else 1, m + 1):
                yield rest + [i]


x = 0
for seq in allseq(n, m):
    s = 0
    for a, b, c, d in lst:
        if seq[b - 1] - seq[a - 1] == c:
            s += d
    x = max(x, s)
print(x)

# C - Many Requirements
from itertools import combinations_with_replacement

n, m, q = list(map(int, input().split()))
lst = []
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    lst.append((a, b, c, d))
x = max(
    sum(d for a, b, c, d in lst if seq[b - 1] - seq[a - 1] == c)
    for seq in combinations_with_replacement(list(range(1, m + 1)), n)
)
print(x)

+from itertools import combinations_with_replacement + -    lst.append((a, b, c, d))  # 得点を先頭に - - -def allseq(n, m): -    if n == 0: -        yield [] -    else: -        for rest in allseq(n - 1, m): -            for i in range(rest[-1] if len(rest) > 0 else 1, m + 1): -                yield rest + [i] - - -x = 0 -for seq in allseq(n, m): -    s = 0 -    for a, b, c, d in lst: -        if seq[b - 1] - seq[a - 1] == c: -            s += d -    x = max(x, s) +    lst.append((a, b, c, d)) +x = max( +    sum(d for a, b, c, d in lst if seq[b - 1] - seq[a - 1] == c) +    for seq in combinations_with_replacement(list(range(1, m + 1)), n) +)