import itertools
from collections import deque


def main():
    N, M, Q = map(int, input().split())
    ls = []
    for _ in range(Q):
        a, b, c, d = map(int, input().split())
        ls.append([a, b, c, d])

    m = list(range(1, M + 1))
    As = []
    A = [0 for _ in range(N)]
    for i in m:
        # i = 2
        A[0] = i
        sub_m = m[i - 1:]
        for j in sub_m:
            # j = 3
            A[1] = j
            sub_m = m[j - 1:]

            if N == 2:
                As.append(A)
                A = A.copy()
                continue

            for k in sub_m:
                # k=3
                A[2] = k
                sub_m = m[k - 1:]

                if N == 3:
                    As.append(A)
                    A = A.copy()
                    continue

                for n in sub_m:
                    # k=3
                    A[3] = n
                    sub_m = m[n - 1:]

                    if N == 4:
                        As.append(A)
                        A = A.copy()
                        continue

                    for o in sub_m:
                        # k=3
                        A[4] = o
                        sub_m = m[o - 1:]

                        if N == 5:
                            As.append(A)
                            A = A.copy()
                            continue

                        for p in sub_m:
                            # k=3
                            A[5] = p
                            sub_m = m[p - 1:]

                            if N == 6:
                                As.append(A)
                                A = A.copy()
                                continue

                            for q in sub_m:
                                # k=3
                                A[6] = q
                                sub_m = m[q - 1:]

                                if N == 7:
                                    As.append(A)
                                    A = A.copy()
                                    continue

                                for r in sub_m:
                                    # k=3
                                    A[7] = r
                                    sub_m = m[r - 1:]

                                    if N == 8:
                                        As.append(A)
                                        A = A.copy()
                                        continue

                                    for s in sub_m:
                                        # k=3
                                        A[8] = s
                                        sub_m = m[s - 1:]

                                        if N == 9:
                                            As.append(A)
                                            A = A.copy()
                                            continue

                                        for t in sub_m:
                                            # k=3
                                            A[9] = t
                                            sub_m = m[t - 1:]

                                            As.append(A)
                                            A = A.copy()

    ans = 0
    for A in As:
        # i=0; A = As[i]
        score = 0
        for i, l in enumerate(ls):
            # i=0; l=ls[i]
            if A[l[1] - 1] - A[l[0] - 1] == l[2]:
                score += l[3]
        if score >= ans:
            ans = score

    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 85636