N,M,Q=map(int,input().split())
ABCD=[]
for i in range(Q):
    a,b,c,d=map(int,input().split())
    ABCD.append([a,b,c,d])

ans=0

def henkan(A,ABCD):
    cnt=0
    for i in range(len(ABCD)):
        a=ABCD[i][0]
        b=ABCD[i][1]
        c=ABCD[i][2]
        d=ABCD[i][3]
        if A[b-1]-A[a-1]==c:
            cnt+=int(d)
    return cnt

A=[0]*N
for q in range(1,M+1):
    A[0]=q
    for w in range(A[0],M+1):
        A[1]=w
        if N==2:
            if ans<=henkan(A,ABCD):
                ans=henkan(A,ABCD)
            else:
                pass
        else:
            for e in range(A[1],M+1):
                A[2]=e
                if N==3:
                    if ans<=henkan(A,ABCD):
                        ans=henkan(A,ABCD)
                    else:
                        pass
                else:
                    for r in range(A[2],M+1):
                        A[3]=r
                        if N==4:
                            if ans<=henkan(A,ABCD):
                                ans=henkan(A,ABCD)
                            else:
                                pass
                        else:
                            for t in range(A[3],M+1):
                                A[4]=t
                                if N==5:
                                    if ans<=henkan(A,ABCD):
                                        ans=henkan(A,ABCD)
                                    else:
                                        pass
                                else:
                                    for y in range(A[4],M+1):
                                        A[5]=y
                                        if N==6:
                                            if ans<=henkan(A,ABCD):
                                                ans=henkan(A,ABCD)
                                            else:
                                                pass
                                        else:
                                            for u in range(A[5],M+1):
                                                A[6]=u
                                                if N==7:
                                                    if ans<=henkan(A,ABCD):
                                                        ans=henkan(A,ABCD)
                                                    else:
                                                        pass
                                                else:
                                                    for i in range(A[6],M+1):
                                                        A[7]=i
                                                        if N==8:
                                                            if ans<=henkan(A,ABCD):
                                                                ans=henkan(A,ABCD)
                                                            else:
                                                                pass
                                                        else:
                                                            for o in range(A[7],M+1):
                                                                A[8]=o
                                                                if N==9:
                                                                    if ans<=henkan(A,ABCD):
                                                                        ans=henkan(A,ABCD)
                                                                    else:
                                                                        pass
                                                                else:
                                                                    for p in range(A[8],M+1):
                                                                        A[9]=p
                                                                        if N==10:
                                                                            if ans<=henkan(A,ABCD):
                                                                                ans=henkan(A,ABCD)
                                                                            else:
                                                                                pass
print(ans)


Measured memory_bytes: 9348