from itertools import combinations_with_replacement

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

a=[0]*q

b=[0]*q

c=[0]*q

d=[0]*q

for i in range(q):

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



ans = 0

for cmb in combinations_with_replacement(list(range(1,m+1)),n):  

  tmp = 0

  for i in range(q):

    if cmb[b[i]-1] - cmb[a[i]-1] == c[i]:

      tmp += d[i]

  

  ans = max(ans,tmp)

  

print(ans)  
from itertools import combinations_with_replacement

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

a=[0]*q

b=[0]*q

c=[0]*q

d=[0]*q

for i in range(q):

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

  

ans = 0      

def dfs(A):

  global ans

  if len(A) >= n:    

    tmp = 0

    for i in range(q):

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

        tmp += d[i]

    ans = max(ans,tmp)

  else:

    s = 1

    if len(A)>0:

      s = A[-1]    

    for i in range(s-1,m):

      dfs(A+[i+1])



A = []

dfs(A) 

print(ans)
from itertools import combinations_with_replacement

n, m, q = list(map(int, input().split()))
a = [0] * q
b = [0] * q
c = [0] * q
d = [0] * q
for i in range(q):
    a[i], b[i], c[i], d[i] = list(map(int, input().split()))
ans = 0
for cmb in combinations_with_replacement(list(range(1, m + 1)), n):
    tmp = 0
    for i in range(q):
        if cmb[b[i] - 1] - cmb[a[i] - 1] == c[i]:
            tmp += d[i]
    ans = max(ans, tmp)
print(ans)

from itertools import combinations_with_replacement

n, m, q = list(map(int, input().split()))
a = [0] * q
b = [0] * q
c = [0] * q
d = [0] * q
for i in range(q):
    a[i], b[i], c[i], d[i] = list(map(int, input().split()))
ans = 0


def dfs(A):
    global ans
    if len(A) >= n:
        tmp = 0
        for i in range(q):
            if A[b[i] - 1] - A[a[i] - 1] == c[i]:
                tmp += d[i]
        ans = max(ans, tmp)
    else:
        s = 1
        if len(A) > 0:
            s = A[-1]
        for i in range(s - 1, m):
            dfs(A + [i + 1])


A = []
dfs(A)
print(ans)

-for cmb in combinations_with_replacement(list(range(1, m + 1)), n): -    tmp = 0 -    for i in range(q): -        if cmb[b[i] - 1] - cmb[a[i] - 1] == c[i]: -            tmp += d[i] -    ans = max(ans, tmp) + + +def dfs(A): +    global ans +    if len(A) >= n: +        tmp = 0 +        for i in range(q): +            if A[b[i] - 1] - A[a[i] - 1] == c[i]: +                tmp += d[i] +        ans = max(ans, tmp) +    else: +        s = 1 +        if len(A) > 0: +            s = A[-1] +        for i in range(s - 1, m): +            dfs(A + [i + 1]) + + +A = [] +dfs(A)