import itertools

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



l = []

for i in range(q):

  a,b,c,d = list(map(int, input().split()))

  l.append((a,b,c,d))

  

comb = itertools.combinations_with_replacement(list(range(1,m+1)), n)



max_score = 0

for e in comb:

  score = 0

  for j in l:

    if e[j[1]-1]-e[j[0]-1]==j[2]:

      

      score += j[3]

  if score > max_score:

    max_score = score

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

 

l = []

for i in range(q):

  a,b,c,d = list(map(int, input().split()))

  l.append((a,b,c,d))

max_score = 0

comb = [[1]]

while len(comb) > 0:

  seed = comb.pop()

  if len(seed) == n:

    score = 0

    for j in l:

      if seed[j[1]-1]-seed[j[0]-1]==j[2]:

      

        score += j[3]

    if score > max_score:

      max_score = score

  else:

    for k in range(seed[-1], m+1):

      s = seed[:]

      s.append(k)

      comb.append(s)

print(max_score)

import itertools

n, m, q = list(map(int, input().split()))
l = []
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    l.append((a, b, c, d))
comb = itertools.combinations_with_replacement(list(range(1, m + 1)), n)
max_score = 0
for e in comb:
    score = 0
    for j in l:
        if e[j[1] - 1] - e[j[0] - 1] == j[2]:
            score += j[3]
    if score > max_score:
        max_score = score
print(max_score)

n, m, q = list(map(int, input().split()))
l = []
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    l.append((a, b, c, d))
max_score = 0
comb = [[1]]
while len(comb) > 0:
    seed = comb.pop()
    if len(seed) == n:
        score = 0
        for j in l:
            if seed[j[1] - 1] - seed[j[0] - 1] == j[2]:
                score += j[3]
        if score > max_score:
            max_score = score
    else:
        for k in range(seed[-1], m + 1):
            s = seed[:]
            s.append(k)
            comb.append(s)
print(max_score)

-import itertools - -comb = itertools.combinations_with_replacement(list(range(1, m + 1)), n) -for e in comb: -    score = 0 -    for j in l: -        if e[j[1] - 1] - e[j[0] - 1] == j[2]: -            score += j[3] -    if score > max_score: -        max_score = score +comb = [[1]] +while len(comb) > 0: +    seed = comb.pop() +    if len(seed) == n: +        score = 0 +        for j in l: +            if seed[j[1] - 1] - seed[j[0] - 1] == j[2]: +                score += j[3] +        if score > max_score: +            max_score = score +    else: +        for k in range(seed[-1], m + 1): +            s = seed[:] +            s.append(k) +            comb.append(s)