from itertools import combinations

import sys

input = lambda: sys.stdin.readline().rstrip()

sys.setrecursionlimit(20000000)



n, m, q = list(map(int, input().split()))

abcd = []

for i in range(q):

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

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



# def check(Q):

#     A = [1] + [-1 for _ in range(n-1)]

#     score = 0

#     for a, b, c, d in Q:

#         if A[a] == -1 and A[b] == -1:

#             tmp_b = min(A[b:])

#             if tmp_b == -1 or tmp_b >= max(A[:a]) + c:

#                 A[a] = max(A[:a])

#                 A[b] = A[a] + c

#                 score += d

#         elif A[a] == -1 and A[b] != -1:

#             if max(A[:a]) <= A[b] - c <= max(A[:a]):

#                 A[a] = A[b] - c

#                 score += d

#         elif A[a] != -1 and A[b] == -1:

#             if min(A[:b]) <= A[a] + c <= max(A[b:]):

#                 A[b] = A[a] + c

#                 score += d

#         else:

#             if A[b] - A[a] == c:

#                 score += d

#         print(A)

#     return score



def dfs(A, depth, max_a):

    if depth == n:

        score = 0

        for a, b, c, d in abcd:

            if A[b] - A[a] == c:

                score += d

        return score

    ans = 0

    for i in range(max_a, m+1):

        ans = max(ans, dfs(A + [i], depth+1, i))

    return ans



ans = dfs([1], 1, 1)

print(ans)



from itertools import combinations

import sys

input = lambda: sys.stdin.readline().rstrip()

sys.setrecursionlimit(20000000)



n, m, q = list(map(int, input().split()))

abcd = []

for i in range(q):

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

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



def dfs(A, depth, max_a):

    if depth == n:

        score = 0

        for a, b, c, d in abcd:

            if A[b] - A[a] == c:

                score += d

        return score

    ans = 0

    for i in range(max_a, m+1):

        ans = max(ans, dfs(A + [i], depth+1, i))

    return ans



ans = dfs([1], 1, 1)

print(ans)



from itertools import combinations
import sys

input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(20000000)
n, m, q = list(map(int, input().split()))
abcd = []
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    abcd.append([a - 1, b - 1, c, d])
# def check(Q):
#     A = [1] + [-1 for _ in range(n-1)]
#     score = 0
#     for a, b, c, d in Q:
#         if A[a] == -1 and A[b] == -1:
#             tmp_b = min(A[b:])
#             if tmp_b == -1 or tmp_b >= max(A[:a]) + c:
#                 A[a] = max(A[:a])
#                 A[b] = A[a] + c
#                 score += d
#         elif A[a] == -1 and A[b] != -1:
#             if max(A[:a]) <= A[b] - c <= max(A[:a]):
#                 A[a] = A[b] - c
#                 score += d
#         elif A[a] != -1 and A[b] == -1:
#             if min(A[:b]) <= A[a] + c <= max(A[b:]):
#                 A[b] = A[a] + c
#                 score += d
#         else:
#             if A[b] - A[a] == c:
#                 score += d
#         print(A)
#     return score
def dfs(A, depth, max_a):
    if depth == n:
        score = 0
        for a, b, c, d in abcd:
            if A[b] - A[a] == c:
                score += d
        return score
    ans = 0
    for i in range(max_a, m + 1):
        ans = max(ans, dfs(A + [i], depth + 1, i))
    return ans


ans = dfs([1], 1, 1)
print(ans)

from itertools import combinations
import sys

input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(20000000)
n, m, q = list(map(int, input().split()))
abcd = []
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    abcd.append([a - 1, b - 1, c, d])


def dfs(A, depth, max_a):
    if depth == n:
        score = 0
        for a, b, c, d in abcd:
            if A[b] - A[a] == c:
                score += d
        return score
    ans = 0
    for i in range(max_a, m + 1):
        ans = max(ans, dfs(A + [i], depth + 1, i))
    return ans


ans = dfs([1], 1, 1)
print(ans)

-# def check(Q): -#     A = [1] + [-1 for _ in range(n-1)] -#     score = 0 -#     for a, b, c, d in Q: -#         if A[a] == -1 and A[b] == -1: -#             tmp_b = min(A[b:]) -#             if tmp_b == -1 or tmp_b >= max(A[:a]) + c: -#                 A[a] = max(A[:a]) -#                 A[b] = A[a] + c -#                 score += d -#         elif A[a] == -1 and A[b] != -1: -#             if max(A[:a]) <= A[b] - c <= max(A[:a]): -#                 A[a] = A[b] - c -#                 score += d -#         elif A[a] != -1 and A[b] == -1: -#             if min(A[:b]) <= A[a] + c <= max(A[b:]): -#                 A[b] = A[a] + c -#                 score += d -#         else: -#             if A[b] - A[a] == c: -#                 score += d -#         print(A) -#     return score + +