n, m, p = map(int, input().split())
A = []
B = []
C = []
D = []
maximum = 0
for i in range(p):
    a, b, c, d = map(int, input().split())
    A.append(a)
    B.append(b)
    C.append(c)
    D.append(d)
for z in range(m):
    for y in range(z, m):
        if n > 2:
            for x in range(y, m):
                if n > 3:
                    for w in range(x, m):
                        if n > 4:
                            for v in range(w, m):
                                if n > 5:
                                    for u in range(v, m):
                                        if n > 6:
                                            for t in range(u, m):
                                                if n > 7:
                                                    for s in range(t, m):
                                                        if n > 8:
                                                            for r in range(s, m):
                                                                if n > 9:
                                                                    for q in range(r, m):
                                                                        L = [z, y, x, w, v, u, t, s, r, q]
                                                                        a = 0
                                                                        for l in range(p):
                                                                            if L[B[l]-1]-L[A[l]-1] == C[l]:
                                                                                a += D[l]
                                                                        maximum = max(maximum, a)
                                                                else:
                                                                    L = [z, y, x, w, v, u, t, s, r]
                                                                    a = 0
                                                                    for l in range(p):
                                                                        if L[B[l]-1]-L[A[l]-1] == C[l]:
                                                                            a += D[l]
                                                                    maximum = max(maximum, a)
                                                        else:
                                                            L = [z, y, x, w, v, u, t, s]
                                                            a = 0
                                                            for l in range(p):
                                                                if L[B[l]-1]-L[A[l]-1] == C[l]:
                                                                    a += D[l]
                                                            maximum = max(maximum, a)
                                                else:
                                                    L = [z, y, x, w, v, u, t]
                                                    a = 0
                                                    for l in range(p):
                                                        if L[B[l]-1]-L[A[l]-1] == C[l]:
                                                            a += D[l]
                                                    maximum = max(maximum, a)
                                        else:
                                            L = [z, y, x, w, v, u]
                                            a = 0
                                            for l in range(p):
                                                if L[B[l]-1]-L[A[l]-1] == C[l]:
                                                    a += D[l]
                                            maximum = max(maximum, a)
                                else:
                                    L = [z, y, x, w, v]
                                    a = 0
                                    for l in range(p):
                                        if L[B[l]-1]-L[A[l]-1] == C[l]:
                                            a += D[l]
                                    maximum = max(maximum, a)
                        else:
                            L = [z, y, x, w]
                            a = 0
                            for l in range(p):
                                if L[B[l]-1]-L[A[l]-1] == C[l]:
                                    a += D[l]
                            maximum = max(maximum, a)
                else:
                    L = [z, y, x]
                    a = 0
                    for l in range(p):
                        if L[B[l]-1]-L[A[l]-1] == C[l]:
                            a += D[l]
                    maximum = max(maximum, a)
        else:
            L = [z, y]
            a = 0
            for l in range(p):
                if L[B[l]-1]-L[A[l]-1] == C[l]:
                    a += D[l]
            maximum = max(maximum, a)
print(maximum)

Measured memory_bytes: 9256