import itertools

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

list1 = [None for i in range(Q)]

for i in range(Q):

  list1[i] = list(map(int,input().split()))



ans = 0

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

  score = 0

  x1 = sorted(x)

  score = sum(y[3] for y in list1 if x1[y[1]-1]-x1[y[0]-1] == y[2])

  if score > ans:

    ans = score



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

list1 = [None for i in range(Q)]



for i in range(Q):

  list1[i] = list(map(int,input().split()))



ans = [0]

def dfs(x,n2,m1):

  if len(x) == n2:

    score = sum(x1[3] for x1 in list1 if x[x1[1]-1]-x[x1[0]-1] == x1[2])

    if score > ans[0]:

      ans[0] = score

  else:

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

      if i >= x[-1]:

        dfs(x+[i],n2,m1)



dfs([1],N,M)

print((ans[0]))
import itertools

N, M, Q = list(map(int, input().split()))
list1 = [None for i in range(Q)]
for i in range(Q):
    list1[i] = list(map(int, input().split()))
ans = 0
for x in itertools.combinations_with_replacement(list(range(1, M + 1)), N):
    score = 0
    x1 = sorted(x)
    score = sum(y[3] for y in list1 if x1[y[1] - 1] - x1[y[0] - 1] == y[2])
    if score > ans:
        ans = score
print(ans)

N, M, Q = list(map(int, input().split()))
list1 = [None for i in range(Q)]
for i in range(Q):
    list1[i] = list(map(int, input().split()))
ans = [0]


def dfs(x, n2, m1):
    if len(x) == n2:
        score = sum(x1[3] for x1 in list1 if x[x1[1] - 1] - x[x1[0] - 1] == x1[2])
        if score > ans[0]:
            ans[0] = score
    else:
        for i in range(1, m1 + 1, 1):
            if i >= x[-1]:
                dfs(x + [i], n2, m1)


dfs([1], N, M)
print((ans[0]))

-import itertools - -ans = 0 -for x in itertools.combinations_with_replacement(list(range(1, M + 1)), N): -    score = 0 -    x1 = sorted(x) -    score = sum(y[3] for y in list1 if x1[y[1] - 1] - x1[y[0] - 1] == y[2]) -    if score > ans: -        ans = score -print(ans) +ans = [0] + + +def dfs(x, n2, m1): +    if len(x) == n2: +        score = sum(x1[3] for x1 in list1 if x[x1[1] - 1] - x[x1[0] - 1] == x1[2]) +        if score > ans[0]: +            ans[0] = score +    else: +        for i in range(1, m1 + 1, 1): +            if i >= x[-1]: +                dfs(x + [i], n2, m1) + + +dfs([1], N, M) +print((ans[0]))