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


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

A=[]

if N==10:
    for a in range(1, M + 1):
        for b in range(a, M + 1):
            for c in range(b, M + 1):
                for d in range(c, M + 1):
                    for e in range(d, M + 1):
                        for f in range(e, M + 1):
                            for g in range(f, M + 1):
                                for h in range(g, M + 1):
                                    for i in range(h, M + 1):
                                        for j in range(i, M + 1):
                                            A.append([a, b, c, d, e, f, g, h, i, j])

elif N==9:
    for a in range(1, M + 1):
        for b in range(a, M + 1):
            for c in range(b, M + 1):
                for d in range(c, M + 1):
                    for e in range(d, M + 1):
                        for f in range(e, M + 1):
                            for g in range(f, M + 1):
                                for h in range(g, M + 1):
                                    for i in range(h, M + 1):
                                        A.append([a, b, c, d, e, f, g, h, i])

elif N==8:
    for a in range(1, M + 1):
        for b in range(a, M + 1):
            for c in range(b, M + 1):
                for d in range(c, M + 1):
                    for e in range(d, M + 1):
                        for f in range(e, M + 1):
                            for g in range(f, M + 1):
                                for h in range(g, M + 1):
                                    A.append([a, b, c, d, e, f, g, h])

elif N == 7:
    for a in range(1, M + 1):
        for b in range(a, M + 1):
            for c in range(b, M + 1):
                for d in range(c, M + 1):
                    for e in range(d, M + 1):
                        for f in range(e, M + 1):
                            for g in range(f, M + 1):
                                A.append([a, b, c, d, e, f, g])

elif N == 6:
    for a in range(1, M + 1):
        for b in range(a, M + 1):
            for c in range(b, M + 1):
                for d in range(c, M + 1):
                    for e in range(d, M + 1):
                        for f in range(e, M + 1):
                            A.append([a, b, c, d, e, f])

elif N == 5:
    for a in range(1, M + 1):
        for b in range(a, M + 1):
            for c in range(b, M + 1):
                for d in range(c, M + 1):
                    for e in range(d, M + 1):
                        A.append([a, b, c, d, e])

elif N == 4:
    for a in range(1, M + 1):
        for b in range(a, M + 1):
            for c in range(b, M + 1):
                for d in range(c, M + 1):
                    A.append([a, b, c, d])

elif N == 3:
    for a in range(1, M + 1):
        for b in range(a, M + 1):
            for c in range(b, M + 1):
                A.append([a, b, c])

elif N == 2:
    for a in range(1, M + 1):
        for b in range(a, M + 1):
            A.append([a, b])

d=[0]*len(A)
for s in range(len(A)):
    for i in range(Q):
        if A[s][S[i][1]-1]-A[s][S[i][0]-1]==S[i][2]:
            d[s]=d[s]+S[i][3]

print(max(d))



















Measured memory_bytes: 26636