import itertools

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

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



ans = 0

for A in itertools.combinations_with_replacement(list(range(1, M+1)), N):

    score = 0

    for a, b, c, d in buff:

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

            score += d

    ans = max(ans, score)



print(ans)
# coding: utf-8

import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)



def dfs(A):

    # 再代入のため、グローバル宣言

    global ans

    

    # N桁なら計算

    if len(A) == N:

        score = 0

        for a, b, c, d in buff:

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

                score += d

        ans = max(ans, score)

        return

    

    A.append(A[-1])

    # 再帰

    while A[-1] <= M:

        # コピーを渡す

        dfs(A[:])

        A[-1] += 1





# main

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

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



ans = 0

dfs([1])



print(ans)
import itertools

N, M, Q = list(map(int, input().split()))
buff = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for A in itertools.combinations_with_replacement(list(range(1, M + 1)), N):
    score = 0
    for a, b, c, d in buff:
        if A[b - 1] - A[a - 1] == c:
            score += d
    ans = max(ans, score)
print(ans)

# coding: utf-8
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)


def dfs(A):
    # 再代入のため、グローバル宣言
    global ans
    # N桁なら計算
    if len(A) == N:
        score = 0
        for a, b, c, d in buff:
            if A[b - 1] - A[a - 1] == c:
                score += d
        ans = max(ans, score)
        return
    A.append(A[-1])
    # 再帰
    while A[-1] <= M:
        # コピーを渡す
        dfs(A[:])
        A[-1] += 1


# main
N, M, Q = list(map(int, input().split()))
buff = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
dfs([1])
print(ans)

-import itertools +# coding: utf-8 +import sys +input = sys.stdin.readline +sys.setrecursionlimit(10**7) + + +def dfs(A): +    # 再代入のため、グローバル宣言 +    global ans +    # N桁なら計算 +    if len(A) == N: +        score = 0 +        for a, b, c, d in buff: +            if A[b - 1] - A[a - 1] == c: +                score += d +        ans = max(ans, score) +        return +    A.append(A[-1]) +    # 再帰 +    while A[-1] <= M: +        # コピーを渡す +        dfs(A[:]) +        A[-1] += 1 + + +# main -for A in itertools.combinations_with_replacement(list(range(1, M + 1)), N): -    score = 0 -    for a, b, c, d in buff: -        if A[b - 1] - A[a - 1] == c: -            score += d -    ans = max(ans, score) +dfs([1])