# import numpy as np

# import queue

# import heapq

from itertools import combinations_with_replacement





def main():

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



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



    As = combinations_with_replacement(list(range(1, M+1)), N)



    max_total = 0

    for A in As:

        total = 0



        for i, q in enumerate(queries):

            if A[q[1]-1] - A[q[0]-1] == q[2]:

                total += q[3]



        max_total = max(max_total, total)



    print(max_total)





if __name__ == "__main__":

    main()

from itertools import combinations_with_replacement





def main():

    n_ints, max_int, n_queries = list(map(int, input().split()))



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



    possible_sequences = combinations_with_replacement(list(range(1, max_int+1)), n_ints)



    max_score = 0

    for sequence in possible_sequences:

        score = 0



        for i, q in enumerate(queries):

            a, b, c, d = q

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

                score += d



        max_score = max(max_score, score)



    print(max_score)





if __name__ == "__main__":

    main()

# import numpy as np
# import queue
# import heapq
from itertools import combinations_with_replacement


def main():
    N, M, Q = list(map(int, input().split()))
    queries = [list(map(int, input().split())) for _ in range(Q)]
    As = combinations_with_replacement(list(range(1, M + 1)), N)
    max_total = 0
    for A in As:
        total = 0
        for i, q in enumerate(queries):
            if A[q[1] - 1] - A[q[0] - 1] == q[2]:
                total += q[3]
        max_total = max(max_total, total)
    print(max_total)


if __name__ == "__main__":
    main()

from itertools import combinations_with_replacement


def main():
    n_ints, max_int, n_queries = list(map(int, input().split()))
    queries = [list(map(int, input().split())) for _ in range(n_queries)]
    possible_sequences = combinations_with_replacement(
        list(range(1, max_int + 1)), n_ints
    )
    max_score = 0
    for sequence in possible_sequences:
        score = 0
        for i, q in enumerate(queries):
            a, b, c, d = q
            if sequence[b - 1] - sequence[a - 1] == c:
                score += d
        max_score = max(max_score, score)
    print(max_score)


if __name__ == "__main__":
    main()

-# import numpy as np -# import queue -# import heapq -    N, M, Q = list(map(int, input().split())) -    queries = [list(map(int, input().split())) for _ in range(Q)] -    As = combinations_with_replacement(list(range(1, M + 1)), N) -    max_total = 0 -    for A in As: -        total = 0 +    n_ints, max_int, n_queries = list(map(int, input().split())) +    queries = [list(map(int, input().split())) for _ in range(n_queries)] +    possible_sequences = combinations_with_replacement( +        list(range(1, max_int + 1)), n_ints +    ) +    max_score = 0 +    for sequence in possible_sequences: +        score = 0 -            if A[q[1] - 1] - A[q[0] - 1] == q[2]: -                total += q[3] -        max_total = max(max_total, total) -    print(max_total) +            a, b, c, d = q +            if sequence[b - 1] - sequence[a - 1] == c: +                score += d +        max_score = max(max_score, score) +    print(max_score)