def next_array(array):#辞書式に次の配列を与える関数

  s = -1

  for x in array[::-1]:

    if x < M:

      break

    else:

      s -= 1

    #Mより小さい最後の項を取得

  

  if s >= -N:

    array[s] += 1#その項を１増やす

    if s < -1:

      for i in range(s+1,0):#以降の項を全て同じ値に

        array[i] = array[s]



  return array

 

def score(array, rules):#点数計算

  scr = 0

  for rule in rules:

    if (array[rule[1]-1] - array[rule[0]-1] == rule[2]):

      scr += rule[3]

      

  return scr

 

# input

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

rules = []

 

for i in range(Q):

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

  rules.append([a, b, c, d])

  

array = [1] * N

max_scr = 0

 

while True:

  max_scr = max(max_scr, score(array,rules)) 

  if array[0] == M:#全探索完了

    break 

  array = next_array(array)#次の配列へ

    

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



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



ary = [1 for _ in range(N)]

ans = 0

while True:

  #print(ary)

  pt = 0

  for j in range(Q):

    rq = Rq[j]

    if ary[rq[1]-1]-ary[rq[0]-1] == rq[2]:

      pt += rq[3]

  ans = max(ans,pt)

  

  if ary[0] == M:

    break

  

  #次の配列へ

  for i in range(-1, -N-1, -1):

    if ary[i] < M:

      ary[i] += 1

      tmp = ary[i]

      for l in range(i+1, 0):

        ary[l] = tmp

      break

      

print(ans)

def next_array(array):  # 辞書式に次の配列を与える関数
    s = -1
    for x in array[::-1]:
        if x < M:
            break
        else:
            s -= 1
        # Mより小さい最後の項を取得
    if s >= -N:
        array[s] += 1  # その項を１増やす
        if s < -1:
            for i in range(s + 1, 0):  # 以降の項を全て同じ値に
                array[i] = array[s]
    return array


def score(array, rules):  # 点数計算
    scr = 0
    for rule in rules:
        if array[rule[1] - 1] - array[rule[0] - 1] == rule[2]:
            scr += rule[3]
    return scr


# input
N, M, Q = list(map(int, input().split()))
rules = []
for i in range(Q):
    a, b, c, d = list(map(int, input().split()))
    rules.append([a, b, c, d])
array = [1] * N
max_scr = 0
while True:
    max_scr = max(max_scr, score(array, rules))
    if array[0] == M:  # 全探索完了
        break
    array = next_array(array)  # 次の配列へ
print(max_scr)

N, M, Q = list(map(int, input().split()))
Rq = [list(map(int, input().split())) for _ in range(Q)]
ary = [1 for _ in range(N)]
ans = 0
while True:
    # print(ary)
    pt = 0
    for j in range(Q):
        rq = Rq[j]
        if ary[rq[1] - 1] - ary[rq[0] - 1] == rq[2]:
            pt += rq[3]
    ans = max(ans, pt)
    if ary[0] == M:
        break
    # 次の配列へ
    for i in range(-1, -N - 1, -1):
        if ary[i] < M:
            ary[i] += 1
            tmp = ary[i]
            for l in range(i + 1, 0):
                ary[l] = tmp
            break
print(ans)

-def next_array(array):  # 辞書式に次の配列を与える関数 -    s = -1 -    for x in array[::-1]: -        if x < M: +N, M, Q = list(map(int, input().split())) +Rq = [list(map(int, input().split())) for _ in range(Q)] +ary = [1 for _ in range(N)] +ans = 0 +while True: +    # print(ary) +    pt = 0 +    for j in range(Q): +        rq = Rq[j] +        if ary[rq[1] - 1] - ary[rq[0] - 1] == rq[2]: +            pt += rq[3] +    ans = max(ans, pt) +    if ary[0] == M: +        break +    # 次の配列へ +    for i in range(-1, -N - 1, -1): +        if ary[i] < M: +            ary[i] += 1 +            tmp = ary[i] +            for l in range(i + 1, 0): +                ary[l] = tmp -        else: -            s -= 1 -        # Mより小さい最後の項を取得 -    if s >= -N: -        array[s] += 1  # その項を１増やす -        if s < -1: -            for i in range(s + 1, 0):  # 以降の項を全て同じ値に -                array[i] = array[s] -    return array - - -def score(array, rules):  # 点数計算 -    scr = 0 -    for rule in rules: -        if array[rule[1] - 1] - array[rule[0] - 1] == rule[2]: -            scr += rule[3] -    return scr - - -# input -N, M, Q = list(map(int, input().split())) -rules = [] -for i in range(Q): -    a, b, c, d = list(map(int, input().split())) -    rules.append([a, b, c, d]) -array = [1] * N -max_scr = 0 -while True: -    max_scr = max(max_scr, score(array, rules)) -    if array[0] == M:  # 全探索完了 -        break -    array = next_array(array)  # 次の配列へ -print(max_scr) +print(ans)