from itertools import combinations_with_replacement



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

abcd = [[int(i) for i in input().split()] for _ in range(q)]

s = 0

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

    t = 0

    for a, b, c, d in abcd:

        t += d * (p[b - 1] - p[a - 1] == c)

    s = max(s, t)

print(s)
from itertools import combinations_with_replacement



def main():

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

    abcd = [[int(i) for i in input().split()] for _ in range(q)]

    s = 0

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

        t = 0

        for a, b, c, d in abcd:

            t += d * (p[b - 1] - p[a - 1] == c)

        s = max(s, t)

    print(s)



if __name__ == '__main__':

    main()
from itertools import combinations_with_replacement

n, m, q = list(map(int, input().split()))
abcd = [[int(i) for i in input().split()] for _ in range(q)]
s = 0
for p in combinations_with_replacement(list(range(1, m + 1)), n):
    t = 0
    for a, b, c, d in abcd:
        t += d * (p[b - 1] - p[a - 1] == c)
    s = max(s, t)
print(s)

from itertools import combinations_with_replacement


def main():
    n, m, q = list(map(int, input().split()))
    abcd = [[int(i) for i in input().split()] for _ in range(q)]
    s = 0
    for p in combinations_with_replacement(list(range(1, m + 1)), n):
        t = 0
        for a, b, c, d in abcd:
            t += d * (p[b - 1] - p[a - 1] == c)
        s = max(s, t)
    print(s)


if __name__ == "__main__":
    main()

-n, m, q = list(map(int, input().split())) -abcd = [[int(i) for i in input().split()] for _ in range(q)] -s = 0 -for p in combinations_with_replacement(list(range(1, m + 1)), n): -    t = 0 -    for a, b, c, d in abcd: -        t += d * (p[b - 1] - p[a - 1] == c) -    s = max(s, t) -print(s) + +def main(): +    n, m, q = list(map(int, input().split())) +    abcd = [[int(i) for i in input().split()] for _ in range(q)] +    s = 0 +    for p in combinations_with_replacement(list(range(1, m + 1)), n): +        t = 0 +        for a, b, c, d in abcd: +            t += d * (p[b - 1] - p[a - 1] == c) +        s = max(s, t) +    print(s) + + +if __name__ == "__main__": +    main()