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

req = [list(map(int, input().split())) for i in range(q)]

  

lst = []



if n == 2:

  for a in range(1,m+1):

    for b in range(a, m+1):

      lst.append([a, b])

    

if n == 3:

  for a in range(1,m+1):

    for b in range(a, m+1):

      for c in range(b, m+1):

        lst.append([a, b, c])



if n == 4:

  for a in range(1,m+1):

    for b in range(a, m+1):

      for c in range(b, m+1):

        for d in range(c, m+1):

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



if n == 5:

  for a in range(1,m+1):

    for b in range(a, m+1):

      for c in range(b, m+1):

        for d in range(c, m+1):

          for e in range(d, m+1):

            lst.append([a, b, c, d, e])



if n == 6:

  for a in range(1,m+1):

    for b in range(a, m+1):

      for c in range(b, m+1):

        for d in range(c, m+1):

          for e in range(d, m+1):

            for f in range(e, m+1):

              lst.append([a, b, c, d, e, f])

                      

if n == 7:

  for a in range(1,m+1):

    for b in range(a, m+1):

      for c in range(b, m+1):

        for d in range(c, m+1):

          for e in range(d, m+1):

            for f in range(e, m+1):

              for g in range(f, m+1):      

                lst.append([a, b, c, d, e, f, g])

                      

if n == 8:

  for a in range(1,m+1):

    for b in range(a, m+1):

      for c in range(b, m+1):

        for d in range(c, m+1):

          for e in range(d, m+1):

            for f in range(e, m+1):

              for g in range(f, m+1):

                for h in range(g, m+1):

                  lst.append([a, b, c, d, e, f, g, h])

                      

if n == 9:

  for a in range(1,m+1):

    for b in range(a, m+1):

      for c in range(b, m+1):

        for d in range(c, m+1):

          for e in range(d, m+1):

            for f in range(e, m+1):

              for g in range(f, m+1):

                for h in range(g, m+1):

                  for i in range(h, m+1):

                    lst.append([a, b, c, d, e, f, g, h, i])

                      

if n == 10:

  for a in range(1,m+1):

    for b in range(a, m+1):

      for c in range(b, m+1):

        for d in range(c, m+1):

          for e in range(d, m+1):

            for f in range(e, m+1):

              for g in range(f, m+1):

                for h in range(g, m+1):

                  for i in range(h, m+1):

                    for j in range(i, m+1):

                      lst.append([a, b, c, d, e, f, g, h, i, j])

        

ans = 0        

for i in range(len(lst)):

  res = 0

  for j in range(q):

    if lst[i][req[j][1]-1] - lst[i][req[j][0]-1] == req[j][2]:

      res += req[j][3]

  ans = max(ans, res)



print(ans)

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

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

maxi = 0

def dfs(A):

  global maxi

  

  if len(A) == N:  # 終端条件 --- 10 重ループまで回したら処理して打ち切り

    ans = 0

    for i in range(Q):

      if A[req[i][1]-1] - A[req[i][0]-1] == req[i][2]:

        ans += req[i][3]

        

    maxi = max(maxi, ans)

    return

      

  if len(A) == 0:

    for v in range(1, M+1):

      A.append(v)

      dfs(A)

      A.pop()

      

  else:    

    for v in range(A[-1], M+1):

      A.append(v)

      dfs(A)

      A.pop() # これが結構ポイント



dfs([])

print(maxi)
n, m, q = list(map(int, input().split()))
req = [list(map(int, input().split())) for i in range(q)]
lst = []
if n == 2:
    for a in range(1, m + 1):
        for b in range(a, m + 1):
            lst.append([a, b])
if n == 3:
    for a in range(1, m + 1):
        for b in range(a, m + 1):
            for c in range(b, m + 1):
                lst.append([a, b, c])
if n == 4:
    for a in range(1, m + 1):
        for b in range(a, m + 1):
            for c in range(b, m + 1):
                for d in range(c, m + 1):
                    lst.append([a, b, c, d])
if n == 5:
    for a in range(1, m + 1):
        for b in range(a, m + 1):
            for c in range(b, m + 1):
                for d in range(c, m + 1):
                    for e in range(d, m + 1):
                        lst.append([a, b, c, d, e])
if n == 6:
    for a in range(1, m + 1):
        for b in range(a, m + 1):
            for c in range(b, m + 1):
                for d in range(c, m + 1):
                    for e in range(d, m + 1):
                        for f in range(e, m + 1):
                            lst.append([a, b, c, d, e, f])
if n == 7:
    for a in range(1, m + 1):
        for b in range(a, m + 1):
            for c in range(b, m + 1):
                for d in range(c, m + 1):
                    for e in range(d, m + 1):
                        for f in range(e, m + 1):
                            for g in range(f, m + 1):
                                lst.append([a, b, c, d, e, f, g])
if n == 8:
    for a in range(1, m + 1):
        for b in range(a, m + 1):
            for c in range(b, m + 1):
                for d in range(c, m + 1):
                    for e in range(d, m + 1):
                        for f in range(e, m + 1):
                            for g in range(f, m + 1):
                                for h in range(g, m + 1):
                                    lst.append([a, b, c, d, e, f, g, h])
if n == 9:
    for a in range(1, m + 1):
        for b in range(a, m + 1):
            for c in range(b, m + 1):
                for d in range(c, m + 1):
                    for e in range(d, m + 1):
                        for f in range(e, m + 1):
                            for g in range(f, m + 1):
                                for h in range(g, m + 1):
                                    for i in range(h, m + 1):
                                        lst.append([a, b, c, d, e, f, g, h, i])
if n == 10:
    for a in range(1, m + 1):
        for b in range(a, m + 1):
            for c in range(b, m + 1):
                for d in range(c, m + 1):
                    for e in range(d, m + 1):
                        for f in range(e, m + 1):
                            for g in range(f, m + 1):
                                for h in range(g, m + 1):
                                    for i in range(h, m + 1):
                                        for j in range(i, m + 1):
                                            lst.append([a, b, c, d, e, f, g, h, i, j])
ans = 0
for i in range(len(lst)):
    res = 0
    for j in range(q):
        if lst[i][req[j][1] - 1] - lst[i][req[j][0] - 1] == req[j][2]:
            res += req[j][3]
    ans = max(ans, res)
print(ans)

N, M, Q = list(map(int, input().split()))
req = [list(map(int, input().split())) for i in range(Q)]
maxi = 0


def dfs(A):
    global maxi
    if len(A) == N:  # 終端条件 --- 10 重ループまで回したら処理して打ち切り
        ans = 0
        for i in range(Q):
            if A[req[i][1] - 1] - A[req[i][0] - 1] == req[i][2]:
                ans += req[i][3]
        maxi = max(maxi, ans)
        return
    if len(A) == 0:
        for v in range(1, M + 1):
            A.append(v)
            dfs(A)
            A.pop()
    else:
        for v in range(A[-1], M + 1):
            A.append(v)
            dfs(A)
            A.pop()  # これが結構ポイント


dfs([])
print(maxi)

-n, m, q = list(map(int, input().split())) -req = [list(map(int, input().split())) for i in range(q)] -lst = [] -if n == 2: -    for a in range(1, m + 1): -        for b in range(a, m + 1): -            lst.append([a, b]) -if n == 3: -    for a in range(1, m + 1): -        for b in range(a, m + 1): -            for c in range(b, m + 1): -                lst.append([a, b, c]) -if n == 4: -    for a in range(1, m + 1): -        for b in range(a, m + 1): -            for c in range(b, m + 1): -                for d in range(c, m + 1): -                    lst.append([a, b, c, d]) -if n == 5: -    for a in range(1, m + 1): -        for b in range(a, m + 1): -            for c in range(b, m + 1): -                for d in range(c, m + 1): -                    for e in range(d, m + 1): -                        lst.append([a, b, c, d, e]) -if n == 6: -    for a in range(1, m + 1): -        for b in range(a, m + 1): -            for c in range(b, m + 1): -                for d in range(c, m + 1): -                    for e in range(d, m + 1): -                        for f in range(e, m + 1): -                            lst.append([a, b, c, d, e, f]) -if n == 7: -    for a in range(1, m + 1): -        for b in range(a, m + 1): -            for c in range(b, m + 1): -                for d in range(c, m + 1): -                    for e in range(d, m + 1): -                        for f in range(e, m + 1): -                            for g in range(f, m + 1): -                                lst.append([a, b, c, d, e, f, g]) -if n == 8: -    for a in range(1, m + 1): -        for b in range(a, m + 1): -            for c in range(b, m + 1): -                for d in range(c, m + 1): -                    for e in range(d, m + 1): -                        for f in range(e, m + 1): -                            for g in range(f, m + 1): -                                for h in range(g, m + 1): -                                    lst.append([a, b, c, d, e, f, g, h]) -if n == 9: -    for a in range(1, m + 1): -        for b in range(a, m + 1): -            for c in range(b, m + 1): -                for d in range(c, m + 1): -                    for e in range(d, m + 1): -                        for f in range(e, m + 1): -                            for g in range(f, m + 1): -                                for h in range(g, m + 1): -                                    for i in range(h, m + 1): -                                        lst.append([a, b, c, d, e, f, g, h, i]) -if n == 10: -    for a in range(1, m + 1): -        for b in range(a, m + 1): -            for c in range(b, m + 1): -                for d in range(c, m + 1): -                    for e in range(d, m + 1): -                        for f in range(e, m + 1): -                            for g in range(f, m + 1): -                                for h in range(g, m + 1): -                                    for i in range(h, m + 1): -                                        for j in range(i, m + 1): -                                            lst.append([a, b, c, d, e, f, g, h, i, j]) -ans = 0 -for i in range(len(lst)): -    res = 0 -    for j in range(q): -        if lst[i][req[j][1] - 1] - lst[i][req[j][0] - 1] == req[j][2]: -            res += req[j][3] -    ans = max(ans, res) -print(ans) +N, M, Q = list(map(int, input().split())) +req = [list(map(int, input().split())) for i in range(Q)] +maxi = 0 + + +def dfs(A): +    global maxi +        ans = 0 +        for i in range(Q): +            if A[req[i][1] - 1] - A[req[i][0] - 1] == req[i][2]: +                ans += req[i][3] +        maxi = max(maxi, ans) +        return +    if len(A) == 0: +        for v in range(1, M + 1): +            A.append(v) +            dfs(A) +            A.pop() +    else: +        for v in range(A[-1], M + 1): +            A.append(v) +            dfs(A) +            A.pop()  # これが結構ポイント + + +dfs([]) +print(maxi)