from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))

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

ans = 0

for x in combinations_with_replacement(list(range(1, M+1)), N):

    point = 0

    for a, b, c, d in abcd:

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

            point += d

    if ans < point:

        ans = point

print(ans)

from itertools import combinations_with_replacement

def main():

    N, M, Q = list(map(int, input().split()))

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

    ans = 0

    for x in combinations_with_replacement(list(range(1, M+1)), N):

        point = 0

        for a, b, c, d in abcd:

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

                point += d

        if ans < point:

            ans = point

    print(ans)



if __name__ == '__main__':

    main()

from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for x in combinations_with_replacement(list(range(1, M + 1)), N):
    point = 0
    for a, b, c, d in abcd:
        if x[b - 1] - x[a - 1] == c:
            point += d
    if ans < point:
        ans = point
print(ans)

from itertools import combinations_with_replacement


def main():
    N, M, Q = list(map(int, input().split()))
    abcd = [list(map(int, input().split())) for _ in range(Q)]
    ans = 0
    for x in combinations_with_replacement(list(range(1, M + 1)), N):
        point = 0
        for a, b, c, d in abcd:
            if x[b - 1] - x[a - 1] == c:
                point += d
        if ans < point:
            ans = point
    print(ans)


if __name__ == "__main__":
    main()

-N, M, Q = list(map(int, input().split())) -abcd = [list(map(int, input().split())) for _ in range(Q)] -ans = 0 -for x in combinations_with_replacement(list(range(1, M + 1)), N): -    point = 0 -    for a, b, c, d in abcd: -        if x[b - 1] - x[a - 1] == c: -            point += d -    if ans < point: -        ans = point -print(ans) + +def main(): +    N, M, Q = list(map(int, input().split())) +    abcd = [list(map(int, input().split())) for _ in range(Q)] +    ans = 0 +    for x in combinations_with_replacement(list(range(1, M + 1)), N): +        point = 0 +        for a, b, c, d in abcd: +            if x[b - 1] - x[a - 1] == c: +                point += d +        if ans < point: +            ans = point +    print(ans) + + +if __name__ == "__main__": +    main()