import itertools



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

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



A_cands = list(itertools.combinations_with_replacement(list(range(1, M+1)), N))       



ans = 0

for cand in A_cands:

    cand_score = 0

    for i in range(Q):

        a, b, c, d = G[i]

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

            cand_score += d

    ans = max(cand_score, ans)

print(ans)

        
import itertools

import numpy as np



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

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



A_cands = np.array(list(itertools.combinations_with_replacement(list(range(1, M+1)), N)))      



scores = np.zeros(len(A_cands), dtype=np.int64)



for i in range(Q):

    a, b, c, d = G[i]

    cond = A_cands[:, b - 1] - A_cands[:, a - 1] == c

    scores += cond * d

print((scores.max()))
import itertools

N, M, Q = list(map(int, input().split()))
G = [list(map(int, input().split())) for i in range(Q)]
A_cands = list(itertools.combinations_with_replacement(list(range(1, M + 1)), N))
ans = 0
for cand in A_cands:
    cand_score = 0
    for i in range(Q):
        a, b, c, d = G[i]
        if cand[b - 1] - cand[a - 1] == c:
            cand_score += d
    ans = max(cand_score, ans)
print(ans)

import itertools
import numpy as np

N, M, Q = list(map(int, input().split()))
G = [list(map(int, input().split())) for i in range(Q)]
A_cands = np.array(
    list(itertools.combinations_with_replacement(list(range(1, M + 1)), N))
)
scores = np.zeros(len(A_cands), dtype=np.int64)
for i in range(Q):
    a, b, c, d = G[i]
    cond = A_cands[:, b - 1] - A_cands[:, a - 1] == c
    scores += cond * d
print((scores.max()))

+import numpy as np -A_cands = list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)) -ans = 0 -for cand in A_cands: -    cand_score = 0 -    for i in range(Q): -        a, b, c, d = G[i] -        if cand[b - 1] - cand[a - 1] == c: -            cand_score += d -    ans = max(cand_score, ans) -print(ans) +A_cands = np.array( +    list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)) +) +scores = np.zeros(len(A_cands), dtype=np.int64) +for i in range(Q): +    a, b, c, d = G[i] +    cond = A_cands[:, b - 1] - A_cands[:, a - 1] == c +    scores += cond * d +print((scores.max()))