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



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



l = []

def dfs(lis, y):

  if len(lis) == N:

    l.append(lis)

    return

  else:

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

      z = list(lis)

      z.append(i)

      dfs(z, i)

    return

      

dfs([], 1)



ans = 0

for A in l:

  tmp = 0

  for a, b, c, d in query:

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

      tmp += d

  ans = max(ans, tmp)



print(ans)
from itertools import combinations_with_replacement



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

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



ans = 0

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

  tmp = 0

  for a, b, c, d in query:

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

      tmp += d

  ans = max(ans, tmp)



print(ans)
N, M, Q = list(map(int, input().split()))
query = [list(map(int, input().split())) for _ in range(Q)]
l = []


def dfs(lis, y):
    if len(lis) == N:
        l.append(lis)
        return
    else:
        for i in range(y, M + 1):
            z = list(lis)
            z.append(i)
            dfs(z, i)
        return


dfs([], 1)
ans = 0
for A in l:
    tmp = 0
    for a, b, c, d in query:
        if A[b - 1] - A[a - 1] == c:
            tmp += d
    ans = max(ans, tmp)
print(ans)

from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))
query = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for A in combinations_with_replacement(list(range(1, M + 1)), N):
    tmp = 0
    for a, b, c, d in query:
        if A[b - 1] - A[a - 1] == c:
            tmp += d
    ans = max(ans, tmp)
print(ans)

+from itertools import combinations_with_replacement + -l = [] - - -def dfs(lis, y): -    if len(lis) == N: -        l.append(lis) -        return -    else: -        for i in range(y, M + 1): -            z = list(lis) -            z.append(i) -            dfs(z, i) -        return - - -dfs([], 1) -for A in l: +for A in combinations_with_replacement(list(range(1, M + 1)), N):