def main():

    from itertools import combinations_with_replacement



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



    conds = []

    for _ in range(Q):

        a, b, c, d = list(map(int, input().split()))

        a -= 1

        b -= 1

        conds.append((a, b, c, d))



    ans = 0

    for temp_A in combinations_with_replacement(iterable=list(range(1, M + 1)), r=N):

        # print(*temp_A)

        t = 0

        for ai, bi, ci, di in conds:

            c = temp_A[bi] - temp_A[ai]

            if c == ci:

                t += di

        ans = max(ans, t)

    print(ans)





if __name__ == '__main__':

    main()



# def choose(n, r):

#     ret = 1

#     for i in range(r):

#         ret *= n - i

#     for i in range(r):

#         ret //= (i + 1)

#     print(ret)

#

# print(choose(20, 10))

# # 184756

# https://atcoder.jp/contests/abc165/submissions/12605117

# 写経



def main():

    from itertools import combinations_with_replacement

    import numpy as np



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



    abcd = []

    for _ in range(Q):

        a, b, c, d = list(map(int, input().split()))

        abcd.append((a - 1, b - 1, c, d))



    temp_As = np.array(

        tuple(combinations_with_replacement(

            iterable=list(range(1, M + 1)),

            r=N

        )), dtype=np.int32

    )



    scores = np.zeros(len(temp_As), dtype=np.int32)

    for a, b, c, d in abcd:

        cond = temp_As[:, b] - temp_As[:, a] == c

        scores += d * cond



    print((scores.max()))





if __name__ == '__main__':

    main()



# a = np.array([0] * 4)

# a += range(4)

# # array([0, 1, 2, 3])

def main():
    from itertools import combinations_with_replacement

    N, M, Q = list(map(int, input().split()))
    conds = []
    for _ in range(Q):
        a, b, c, d = list(map(int, input().split()))
        a -= 1
        b -= 1
        conds.append((a, b, c, d))
    ans = 0
    for temp_A in combinations_with_replacement(iterable=list(range(1, M + 1)), r=N):
        # print(*temp_A)
        t = 0
        for ai, bi, ci, di in conds:
            c = temp_A[bi] - temp_A[ai]
            if c == ci:
                t += di
        ans = max(ans, t)
    print(ans)


if __name__ == "__main__":
    main()
# def choose(n, r):
#     ret = 1
#     for i in range(r):
#         ret *= n - i
#     for i in range(r):
#         ret //= (i + 1)
#     print(ret)
#
# print(choose(20, 10))
# # 184756

# https://atcoder.jp/contests/abc165/submissions/12605117
# 写経
def main():
    from itertools import combinations_with_replacement
    import numpy as np

    N, M, Q = list(map(int, input().split()))
    abcd = []
    for _ in range(Q):
        a, b, c, d = list(map(int, input().split()))
        abcd.append((a - 1, b - 1, c, d))
    temp_As = np.array(
        tuple(combinations_with_replacement(iterable=list(range(1, M + 1)), r=N)),
        dtype=np.int32,
    )
    scores = np.zeros(len(temp_As), dtype=np.int32)
    for a, b, c, d in abcd:
        cond = temp_As[:, b] - temp_As[:, a] == c
        scores += d * cond
    print((scores.max()))


if __name__ == "__main__":
    main()
# a = np.array([0] * 4)
# a += range(4)
# # array([0, 1, 2, 3])

+# https://atcoder.jp/contests/abc165/submissions/12605117 +# 写経 +    import numpy as np -    conds = [] +    abcd = [] -        a -= 1 -        b -= 1 -        conds.append((a, b, c, d)) -    ans = 0 -    for temp_A in combinations_with_replacement(iterable=list(range(1, M + 1)), r=N): -        # print(*temp_A) -        t = 0 -        for ai, bi, ci, di in conds: -            c = temp_A[bi] - temp_A[ai] -            if c == ci: -                t += di -        ans = max(ans, t) -    print(ans) +        abcd.append((a - 1, b - 1, c, d)) +    temp_As = np.array( +        tuple(combinations_with_replacement(iterable=list(range(1, M + 1)), r=N)), +        dtype=np.int32, +    ) +    scores = np.zeros(len(temp_As), dtype=np.int32) +    for a, b, c, d in abcd: +        cond = temp_As[:, b] - temp_As[:, a] == c +        scores += d * cond +    print((scores.max())) -# def choose(n, r): -#     ret = 1 -#     for i in range(r): -#         ret *= n - i -#     for i in range(r): -#         ret //= (i + 1) -#     print(ret) -# -# print(choose(20, 10)) -# # 184756 +# a = np.array([0] * 4) +# a += range(4) +# # array([0, 1, 2, 3])