#!/usr/bin/env python3

# スペース区切りの整数の入力



from itertools import combinations_with_replacement as comb_rplc



# スペース区切りの整数の入力

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

#配列の入力

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



ans = -1



for seq in comb_rplc(list(range(1, M+1)), N):

    score = 0

    for a, b, c, d in data:

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

            score += d

    

    ans = max(ans, score)



print(ans)

#!/usr/bin/env python3

# スペース区切りの整数の入力



from collections import deque



def calc(seq):

    score = 0

    for a, b, c, d in data:

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

            score += d

    return score



# スペース区切りの整数の入力

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

#配列の入力

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



ans = -1



que = deque()



for i in range(1, M+1):

    que.append([i])





while que:

    seq = que.popleft()



    if len(seq) == N:

        score = calc(seq)

        ans = max(ans, score)

    else:

        for i in range(seq[-1], M+1):

            seq_next = seq + [i]

            que.append(seq_next)



print(ans)

#!/usr/bin/env python3
# スペース区切りの整数の入力
from itertools import combinations_with_replacement as comb_rplc

# スペース区切りの整数の入力
N, M, Q = list(map(int, input().split()))
# 配列の入力
data = [list(map(int, input().split())) for _ in range(Q)]
ans = -1
for seq in comb_rplc(list(range(1, M + 1)), N):
    score = 0
    for a, b, c, d in data:
        if seq[b - 1] - seq[a - 1] == c:
            score += d
    ans = max(ans, score)
print(ans)

#!/usr/bin/env python3
# スペース区切りの整数の入力
from collections import deque


def calc(seq):
    score = 0
    for a, b, c, d in data:
        if seq[b - 1] - seq[a - 1] == c:
            score += d
    return score


# スペース区切りの整数の入力
N, M, Q = list(map(int, input().split()))
# 配列の入力
data = [list(map(int, input().split())) for _ in range(Q)]
ans = -1
que = deque()
for i in range(1, M + 1):
    que.append([i])
while que:
    seq = que.popleft()
    if len(seq) == N:
        score = calc(seq)
        ans = max(ans, score)
    else:
        for i in range(seq[-1], M + 1):
            seq_next = seq + [i]
            que.append(seq_next)
print(ans)

-from itertools import combinations_with_replacement as comb_rplc +from collections import deque + + +def calc(seq): +    score = 0 +    for a, b, c, d in data: +        if seq[b - 1] - seq[a - 1] == c: +            score += d +    return score + -for seq in comb_rplc(list(range(1, M + 1)), N): -    score = 0 -    for a, b, c, d in data: -        if seq[b - 1] - seq[a - 1] == c: -            score += d -    ans = max(ans, score) +que = deque() +for i in range(1, M + 1): +    que.append([i]) +while que: +    seq = que.popleft() +    if len(seq) == N: +        score = calc(seq) +        ans = max(ans, score) +    else: +        for i in range(seq[-1], M + 1): +            seq_next = seq + [i] +            que.append(seq_next)