# Original solution
N, M, Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for i in range(Q)]

def check(arr):
    ans = 0
    for A in arr:
        score = 0
        for a, b, c, d in abcd:
            if A[b-1] - A[a-1] == c:
                score += d
        if ans < score:
            ans = score
    return ans

if N == 2:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            A.append(i)
            A.append(j)
            arr.append(A)
            A = []
    ans = check(arr)
elif N == 3:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                A.append(i)
                A.append(j)
                A.append(k)
                arr.append(A)
                A = []
    ans = check(arr)
elif N == 4:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                for l in range(k, M+1):
                    A.append(i)
                    A.append(j)
                    A.append(k)
                    A.append(l)
                    arr.append(A)
                    A = []
    ans = check(arr)
elif N == 5:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                for l in range(k, M+1):
                    for m in range(l, M+1):
                        A.append(i)
                        A.append(j)
                        A.append(k)
                        A.append(l)
                        A.append(m)
                        arr.append(A)
                        A = []
    ans = check(arr)
elif N == 6:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                for l in range(k, M+1):
                    for m in range(l, M+1):
                        for n in range(m, M+1):
                            A.append(i)
                            A.append(j)
                            A.append(k)
                            A.append(l)
                            A.append(m)
                            A.append(n)
                            arr.append(A)
                            A = []
    ans = check(arr)
elif N == 7:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                for l in range(k, M+1):
                    for m in range(l, M+1):
                        for n in range(m, M+1):
                            for o in range(n, M+1):
                                A.append(i)
                                A.append(j)
                                A.append(k)
                                A.append(l)
                                A.append(m)
                                A.append(n)
                                A.append(o)
                                arr.append(A)
                                A = []
    ans = check(arr)
elif N == 8:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                for l in range(k, M+1):
                    for m in range(l, M+1):
                        for n in range(m, M+1):
                            for o in range(n, M+1):
                                for p in range(o, M+1):
                                    A.append(i)
                                    A.append(j)
                                    A.append(k)
                                    A.append(l)
                                    A.append(m)
                                    A.append(n)
                                    A.append(o)
                                    A.append(p)
                                    arr.append(A)
                                    A = []
    ans = check(arr)
elif N == 9:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                for l in range(k, M+1):
                    for m in range(l, M+1):
                        for n in range(m, M+1):
                            for o in range(n, M+1):
                                for p in range(o, M+1):
                                    for q in range(p, M+1):
                                        A.append(i)
                                        A.append(j)
                                        A.append(k)
                                        A.append(l)
                                        A.append(m)
                                        A.append(n)
                                        A.append(o)
                                        A.append(p)
                                        A.append(q)
                                        arr.append(A)
                                        A = []
    ans = check(arr)
elif N == 10:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                for l in range(k, M+1):
                    for m in range(l, M+1):
                        for n in range(m, M+1):
                            for o in range(n, M+1):
                                for p in range(o, M+1):
                                    for q in range(p, M+1):
                                        for r in range(q, M+1):
                                            A.append(i)
                                            A.append(j)
                                            A.append(k)
                                            A.append(l)
                                            A.append(m)
                                            A.append(n)
                                            A.append(o)
                                            A.append(p)
                                            A.append(q)
                                            A.append(r)
                                            arr.append(A)
                                            A = []
    ans = check(arr)

print(ans)




# Optimized solution
N, M, Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for i in range(Q)]

from itertools import combinations_with_replacement as com
ans = 0
for A in com(list(range(1, M+1)), N):
    score = 0
    for a, b, c, d in abcd:
        if A[b-1] - A[a-1] == c:
            score += d
    if ans < score:
        ans = score
print(ans)