import numpy as np

import itertools



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

abcd=[]



for q in range(Q):

  array=list(map(int,input().split()))

  abcd.append(array)



all = list(itertools.combinations_with_replacement(list(range(1,M+1)), N))



MAX=0



for al in all:

  sum=0

  for a,b,c,d in abcd:

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

      sum += d

  MAX=max(sum,MAX)

  

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

a=[];b=[];c=[];d=[]



for q in range(Q):

  a_,b_,c_,d_ =list(map(int,input().split()))

  a.append(a_-1)

  b.append(b_-1)

  c.append(c_)

  d.append(d_)

  

def score(A):

  tmp=0

  for q in range(Q):

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

      tmp += d[q]

  

  return tmp



def dfs(A):

  if len(A)==N:

    return score(A)

  

  elif len(A)==0:

    Alast=1

  

  else:

    Alast=A[-1]

   

  ans=0

    

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

    A.append(i)

    ans = max(ans,dfs(A))

    A.pop()

  

  return ans

    

print((dfs([])))  

  
import numpy as np
import itertools

N, M, Q = list(map(int, input().split()))
abcd = []
for q in range(Q):
    array = list(map(int, input().split()))
    abcd.append(array)
all = list(itertools.combinations_with_replacement(list(range(1, M + 1)), N))
MAX = 0
for al in all:
    sum = 0
    for a, b, c, d in abcd:
        if al[b - 1] - al[a - 1] == c:
            sum += d
    MAX = max(sum, MAX)
print(MAX)

N, M, Q = list(map(int, input().split()))
a = []
b = []
c = []
d = []
for q in range(Q):
    a_, b_, c_, d_ = list(map(int, input().split()))
    a.append(a_ - 1)
    b.append(b_ - 1)
    c.append(c_)
    d.append(d_)


def score(A):
    tmp = 0
    for q in range(Q):
        if A[b[q]] - A[a[q]] == c[q]:
            tmp += d[q]
    return tmp


def dfs(A):
    if len(A) == N:
        return score(A)
    elif len(A) == 0:
        Alast = 1
    else:
        Alast = A[-1]
    ans = 0
    for i in range(Alast, M + 1):
        A.append(i)
        ans = max(ans, dfs(A))
        A.pop()
    return ans


print((dfs([])))

-import numpy as np -import itertools +N, M, Q = list(map(int, input().split())) +a = [] +b = [] +c = [] +d = [] +for q in range(Q): +    a_, b_, c_, d_ = list(map(int, input().split())) +    a.append(a_ - 1) +    b.append(b_ - 1) +    c.append(c_) +    d.append(d_) -N, M, Q = list(map(int, input().split())) -abcd = [] -for q in range(Q): -    array = list(map(int, input().split())) -    abcd.append(array) -all = list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)) -MAX = 0 -for al in all: -    sum = 0 -    for a, b, c, d in abcd: -        if al[b - 1] - al[a - 1] == c: -            sum += d -    MAX = max(sum, MAX) -print(MAX) + +def score(A): +    tmp = 0 +    for q in range(Q): +        if A[b[q]] - A[a[q]] == c[q]: +            tmp += d[q] +    return tmp + + +def dfs(A): +    if len(A) == N: +        return score(A) +    elif len(A) == 0: +        Alast = 1 +    else: +        Alast = A[-1] +    ans = 0 +    for i in range(Alast, M + 1): +        A.append(i) +        ans = max(ans, dfs(A)) +        A.pop() +    return ans + + +print((dfs([])))