N,M,Q = (int(x) for x in input().split())
ABCD  = [0]*Q
for T in range(0,Q):
    ABCD[T] = [int(x) for x in input().split()]
    
MaxP  = 0
for A1 in range(1,M+1):
    for A2 in range(A1,M+1):
        if N<=2:
            Point = 0
            A = [A1,A2]
            for TT in range(0,Q):
                if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                    Point = Point + ABCD[TT][3]
            if Point > MaxP:
                MaxP = Point
        else:
            for A3 in range(A2,M+1):
                if N<=3:
                    Point = 0
                    A = [A1,A2,A3]
                    for TT in range(0,Q):
                        if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                            Point = Point + ABCD[TT][3]
                    if Point > MaxP:
                        MaxP = Point                    
                else:
                    for A4 in range(A3,M+1):
                        if N<=4:
                            Point = 0
                            A = [A1,A2,A3,A4]
                            for TT in range(0,Q):
                                if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                    Point = Point + ABCD[TT][3]
                            if Point > MaxP:
                                MaxP = Point
                        else:
                            for A5 in range(A4,M+1):
                                if N<=5:
                                    Point = 0
                                    A = [A1,A2,A3,A4,A5]
                                    for TT in range(0,Q):
                                        if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                            Point = Point + ABCD[TT][3]
                                    if Point > MaxP:
                                        MaxP = Point                                    
                                else:
                                    for A6 in range(A5,M+1):
                                        if N<=6:
                                            Point = 0
                                            A = [A1,A2,A3,A4,A5,A6]
                                            for TT in range(0,Q):
                                                if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                                    Point = Point + ABCD[TT][3]
                                            if Point > MaxP:
                                                MaxP = Point                                        
                                        else:
                                            for A7 in range(A6,M+1):
                                                if N<=7:
                                                    Point = 0
                                                    A = [A1,A2,A3,A4,A5,A6,A7]
                                                    for TT in range(0,Q):
                                                        if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                                            Point = Point + ABCD[TT][3]
                                                    if Point > MaxP:
                                                        MaxP = Point                                                
                                                else:
                                                    for A8 in range(A7,M+1):
                                                        if N<=8:
                                                            Point = 0
                                                            A = [A1,A2,A3,A4,A5,A6,A7,A8]
                                                            for TT in range(0,Q):
                                                                if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                                                    Point = Point + ABCD[TT][3]
                                                            if Point > MaxP:
                                                                MaxP = Point                                                            
                                                        else:
                                                            for A9 in range(A8,M+1):
                                                                if N<=9:
                                                                    Point = 0
                                                                    A = [A1,A2,A3,A4,A5,A6,A7,A8,A9]
                                                                    for TT in range(0,Q):
                                                                        if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                                                            Point = Point + ABCD[TT][3]
                                                                    if Point > MaxP:
                                                                        MaxP = Point                                                                
                                                                else:
                                                                    for A10 in range(A9,M+1):
                                                                        Point = 0
                                                                        A = [A1,A2,A3,A4,A5,A6,A7,A8,A9,A10]
                                                                        for TT in range(0,Q):
                                                                            if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                                                                Point = Point + ABCD[TT][3]
                                                                        if Point > MaxP:
                                                                            MaxP = Point
print(MaxP)

Measured memory_bytes: 72704