from itertools import combinations_with_replacement



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

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

ans = 0



for A in combinations_with_replacement(list(range(M)), N):

    temp = 0

    for abcd in ABCD:

        if A[abcd[1]-1]+1 == A[abcd[0]-1]+1 + abcd[2]:

            temp += abcd[3]

    if temp > ans:

        ans = temp

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 i in range(Q)]

    ans = 0



    for A in combinations_with_replacement(list(range(M)), N):

        temp = 0

        for abcd in ABCD:

            if A[abcd[1]-1]+1 == A[abcd[0]-1]+1 + abcd[2]:

                temp += abcd[3]

        if temp > ans:

            ans = temp

    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 i in range(Q)]
ans = 0
for A in combinations_with_replacement(list(range(M)), N):
    temp = 0
    for abcd in ABCD:
        if A[abcd[1] - 1] + 1 == A[abcd[0] - 1] + 1 + abcd[2]:
            temp += abcd[3]
    if temp > ans:
        ans = temp
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 i in range(Q)]
    ans = 0
    for A in combinations_with_replacement(list(range(M)), N):
        temp = 0
        for abcd in ABCD:
            if A[abcd[1] - 1] + 1 == A[abcd[0] - 1] + 1 + abcd[2]:
                temp += abcd[3]
        if temp > ans:
            ans = temp
    print(ans)


if __name__ == "__main__":
    main()

-N, M, Q = list(map(int, input().split())) -ABCD = [list(map(int, input().split())) for i in range(Q)] -ans = 0 -for A in combinations_with_replacement(list(range(M)), N): -    temp = 0 -    for abcd in ABCD: -        if A[abcd[1] - 1] + 1 == A[abcd[0] - 1] + 1 + abcd[2]: -            temp += abcd[3] -    if temp > ans: -        ans = temp -print(ans) + +def main(): +    N, M, Q = list(map(int, input().split())) +    ABCD = [list(map(int, input().split())) for i in range(Q)] +    ans = 0 +    for A in combinations_with_replacement(list(range(M)), N): +        temp = 0 +        for abcd in ABCD: +            if A[abcd[1] - 1] + 1 == A[abcd[0] - 1] + 1 + abcd[2]: +                temp += abcd[3] +        if temp > ans: +            ans = temp +    print(ans) + + +if __name__ == "__main__": +    main()