from _collections import deque



def cacl(seq):

    score = 0

    for a, b, c, d in req:

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

            score += d

    return score



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

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



ans = 0

que = deque()



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

    que.append([i])



while que:

    seq = que.popleft()



    if len(seq) == n:

        score = cacl(seq)

        ans = max(ans, score)

    else:

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

            seq_next = seq + [i]

            que.append(seq_next)

print(ans)
def dfs(seq):

    ans = 0



    if len(seq) == n:

        score = 0

        for a, b, c, d in req:

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

                score += d

        return score

    else:

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

            next_seq = seq + (i,)

            score = dfs(next_seq)

            ans = max(ans, score)

    return ans



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

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



print((dfs((1,))))

from _collections import deque


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


n, m, q = list(map(int, input().split()))
req = [list(map(int, input().split())) for _ in range(q)]
ans = 0
que = deque()
for i in range(1, m + 1):
    que.append([i])
while que:
    seq = que.popleft()
    if len(seq) == n:
        score = cacl(seq)
        ans = max(ans, score)
    else:
        for i in range(seq[-1], m + 1):
            seq_next = seq + [i]
            que.append(seq_next)
print(ans)

def dfs(seq):
    ans = 0
    if len(seq) == n:
        score = 0
        for a, b, c, d in req:
            if seq[b - 1] - seq[a - 1] == c:
                score += d
        return score
    else:
        for i in range(seq[-1], m + 1):
            next_seq = seq + (i,)
            score = dfs(next_seq)
            ans = max(ans, score)
    return ans


n, m, q = list(map(int, input().split()))
req = [list(map(int, input().split())) for _ in range(q)]
print((dfs((1,))))

-from _collections import deque - - -def cacl(seq): -    score = 0 -    for a, b, c, d in req: -        if seq[b - 1] - seq[a - 1] == c: -            score += d -    return score +def dfs(seq): +    ans = 0 +    if len(seq) == n: +        score = 0 +        for a, b, c, d in req: +            if seq[b - 1] - seq[a - 1] == c: +                score += d +        return score +    else: +        for i in range(seq[-1], m + 1): +            next_seq = seq + (i,) +            score = dfs(next_seq) +            ans = max(ans, score) +    return ans -ans = 0 -que = deque() -for i in range(1, m + 1): -    que.append([i]) -while que: -    seq = que.popleft() -    if len(seq) == n: -        score = cacl(seq) -        ans = max(ans, score) -    else: -        for i in range(seq[-1], m + 1): -            seq_next = seq + [i] -            que.append(seq_next) -print(ans) +print((dfs((1,))))