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

X = [list(map(int, input().split())) for _ in range(Q)]





def dfs(x):

    if len(x) == N + 1:

        tmp = 0

        for i in range(Q):

            if x[X[i][1]] - x[X[i][0]] == X[i][2]:

                tmp += X[i][3]

        return tmp



    x.append(x[-1])

    res = 0

    while x[-1] <= M:

        res = max(res, dfs(x))

        x[-1] += 1

    x.pop()



    return res



print((dfs([1])))

from itertools import combinations_with_replacement



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

X = [list(map(int, input().split())) for _ in range(Q)]



ans = 0

for x in combinations_with_replacement(list(range(1, M + 1)), N):

    tmp = 0

    for i in range(Q):

        if x[X[i][1] - 1] - x[X[i][0] - 1] == X[i][2]:

            tmp += X[i][3]

    ans = max(ans, tmp)



print(ans)

N, M, Q = list(map(int, input().split()))
X = [list(map(int, input().split())) for _ in range(Q)]


def dfs(x):
    if len(x) == N + 1:
        tmp = 0
        for i in range(Q):
            if x[X[i][1]] - x[X[i][0]] == X[i][2]:
                tmp += X[i][3]
        return tmp
    x.append(x[-1])
    res = 0
    while x[-1] <= M:
        res = max(res, dfs(x))
        x[-1] += 1
    x.pop()
    return res


print((dfs([1])))

from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))
X = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for x in combinations_with_replacement(list(range(1, M + 1)), N):
    tmp = 0
    for i in range(Q):
        if x[X[i][1] - 1] - x[X[i][0] - 1] == X[i][2]:
            tmp += X[i][3]
    ans = max(ans, tmp)
print(ans)

+from itertools import combinations_with_replacement + - - -def dfs(x): -    if len(x) == N + 1: -        tmp = 0 -        for i in range(Q): -            if x[X[i][1]] - x[X[i][0]] == X[i][2]: -                tmp += X[i][3] -        return tmp -    x.append(x[-1]) -    res = 0 -    while x[-1] <= M: -        res = max(res, dfs(x)) -        x[-1] += 1 -    x.pop() -    return res - - -print((dfs([1]))) +ans = 0 +for x in combinations_with_replacement(list(range(1, M + 1)), N): +    tmp = 0 +    for i in range(Q): +        if x[X[i][1] - 1] - x[X[i][0] - 1] == X[i][2]: +            tmp += X[i][3] +    ans = max(ans, tmp) +print(ans)