N, M, Q = map(int,(input().split()))
A=[]

if N==10:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):
                        for a6 in range(a5,M+1):
                            for a7 in range(a6,M+1):
                                for a8 in range(a7,M+1):
                                    for a9 in range(a8,M+1):
                                        for a10 in range(a9,M+1):
                                            A.append([a1,a2,a3,a4,a5,a6,a7,a8,a9,a10])
                                            
if N==9:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):
                        for a6 in range(a5,M+1):
                            for a7 in range(a6,M+1):
                                for a8 in range(a7,M+1):
                                    for a9 in range(a8,M+1):
                                        A.append([a1,a2,a3,a4,a5,a6,a7,a8,a9])
if N==8:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):
                        for a6 in range(a5,M+1):
                            for a7 in range(a6,M+1):
                                for a8 in range(a7,M+1):
                                    A.append([a1,a2,a3,a4,a5,a6,a7,a8])
                                        
if N==7:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):
                        for a6 in range(a5,M+1):
                            for a7 in range(a6,M+1):

                                A.append([a1,a2,a3,a4,a5,a6,a7])
if N==6:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):
                        for a6 in range(a5,M+1):

                            A.append([a1,a2,a3,a4,a5,a6])
if N==5:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):

                        A.append([a1,a2,a3,a4,a5])                                
if N==4:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    

                    A.append([a1,a2,a3,a4])
if N==3:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                    

                A.append([a1,a2,a3])
if N==2:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):                    


            A.append([a1,a2])
ans=0

abcd=[]
for i in range(Q):
    abc = list(map(int,(input().split())))
    abcd.append(abc)
dd=[]
for i in range(len(A)):
    d=0
    for j in range(Q):
        if A[i][abcd[j][1]-1]-A[i][abcd[j][0]-1] == abcd[j][2]:
            d+=abcd[j][3]
    dd.append(d)
print(str(max(dd)))

Measured memory_bytes: 96748