import itertools

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

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

ans = 0

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

  score = 0

  for i in range(Q):

    a = abcd[i][0]

    b = abcd[i][1]

    c = abcd[i][2]

    d = abcd[i][3]

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

      score += d

  ans = max(ans,score)

print(ans)

import itertools

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

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

total = []

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

  score = 0

  for a,b,c,d in abcd:

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

      score += d

    total.append(score)

print((max(total)))

import itertools

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

import itertools

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

-ans = 0 +total = [] -    for i in range(Q): -        a = abcd[i][0] -        b = abcd[i][1] -        c = abcd[i][2] -        d = abcd[i][3] +    for a, b, c, d in abcd: -    ans = max(ans, score) -print(ans) +        total.append(score) +print((max(total)))