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

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

L=[]

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

  if n==1:

    L.append((i))

  else:

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

      if n==2:

        L.append((i,i2))

      else:

        for i3 in range(i2,m+1):

          if n==3:

            L.append((i,i2,i3))

          else:

            for i4 in range(i3,m+1):

              if n==4:

                L.append((i,i2,i3,i4))

              else:

                for i5 in range(i4,m+1):

                  if n==5:

                    L.append((i,i2,i3,i4,i5))

                  else:

                    for i6 in range(i5,m+1):

                      if n==6:

                        L.append((i,i2,i3,i4,i5,i6))

                      else:

                        for i7 in range(i6,m+1):

                          if n==7:

                            L.append((i,i2,i3,i4,i5,i6,i7))

                          else:

                            for i8 in range(i7,m+1):

                              if n==8:

                                L.append((i,i2,i3,i4,i5,i6,i7,i8))

                              else:

                                for i9 in range(i8,m+1):

                                  if n==9:

                                    L.append((i,i2,i3,i4,i5,i6,i7,i8,i9))

                                  else:

                                    for i10 in range(i9,m+1):

                                      L.append((i,i2,i3,i4,i5,i6,i7,i8,i9,i10))

ans=[]

for i in range(len(L)):

  ct=0

  for j in range(q):

    if L[i][l[j][1]-1]-L[i][l[j][0]-1]==l[j][2]:

      ct+=l[j][3]

  ans.append(ct)

print((max(ans)))
import itertools

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

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

M=[i+1 for i in range(m)]

L=[]

for i in itertools.combinations_with_replacement(M,n):

  L.append(i)

ans=[]

for i in range(len(L)):

  ct=0

  for j in range(q):

    if L[i][l[j][1]-1]-L[i][l[j][0]-1]==l[j][2]:

      ct+=l[j][3]

  ans.append(ct)

print((max(ans)))
n, m, q = list(map(int, input().split()))
l = [list(map(int, input().split())) for i in range(q)]
L = []
for i in range(1, m + 1):
    if n == 1:
        L.append((i))
    else:
        for i2 in range(i, m + 1):
            if n == 2:
                L.append((i, i2))
            else:
                for i3 in range(i2, m + 1):
                    if n == 3:
                        L.append((i, i2, i3))
                    else:
                        for i4 in range(i3, m + 1):
                            if n == 4:
                                L.append((i, i2, i3, i4))
                            else:
                                for i5 in range(i4, m + 1):
                                    if n == 5:
                                        L.append((i, i2, i3, i4, i5))
                                    else:
                                        for i6 in range(i5, m + 1):
                                            if n == 6:
                                                L.append((i, i2, i3, i4, i5, i6))
                                            else:
                                                for i7 in range(i6, m + 1):
                                                    if n == 7:
                                                        L.append(
                                                            (i, i2, i3, i4, i5, i6, i7)
                                                        )
                                                    else:
                                                        for i8 in range(i7, m + 1):
                                                            if n == 8:
                                                                L.append(
                                                                    (
                                                                        i,
                                                                        i2,
                                                                        i3,
                                                                        i4,
                                                                        i5,
                                                                        i6,
                                                                        i7,
                                                                        i8,
                                                                    )
                                                                )
                                                            else:
                                                                for i9 in range(
                                                                    i8, m + 1
                                                                ):
                                                                    if n == 9:
                                                                        L.append(
                                                                            (
                                                                                i,
                                                                                i2,
                                                                                i3,
                                                                                i4,
                                                                                i5,
                                                                                i6,
                                                                                i7,
                                                                                i8,
                                                                                i9,
                                                                            )
                                                                        )
                                                                    else:
                                                                        for (
                                                                            i10
                                                                        ) in range(
                                                                            i9, m + 1
                                                                        ):
                                                                            L.append(
                                                                                (
                                                                                    i,
                                                                                    i2,
                                                                                    i3,
                                                                                    i4,
                                                                                    i5,
                                                                                    i6,
                                                                                    i7,
                                                                                    i8,
                                                                                    i9,
                                                                                    i10,
                                                                                )
                                                                            )
ans = []
for i in range(len(L)):
    ct = 0
    for j in range(q):
        if L[i][l[j][1] - 1] - L[i][l[j][0] - 1] == l[j][2]:
            ct += l[j][3]
    ans.append(ct)
print((max(ans)))

import itertools

n, m, q = list(map(int, input().split()))
l = [list(map(int, input().split())) for i in range(q)]
M = [i + 1 for i in range(m)]
L = []
for i in itertools.combinations_with_replacement(M, n):
    L.append(i)
ans = []
for i in range(len(L)):
    ct = 0
    for j in range(q):
        if L[i][l[j][1] - 1] - L[i][l[j][0] - 1] == l[j][2]:
            ct += l[j][3]
    ans.append(ct)
print((max(ans)))

+import itertools + +M = [i + 1 for i in range(m)] -for i in range(1, m + 1): -    if n == 1: -        L.append((i)) -    else: -        for i2 in range(i, m + 1): -            if n == 2: -                L.append((i, i2)) -            else: -                for i3 in range(i2, m + 1): -                    if n == 3: -                        L.append((i, i2, i3)) -                    else: -                        for i4 in range(i3, m + 1): -                            if n == 4: -                                L.append((i, i2, i3, i4)) -                            else: -                                for i5 in range(i4, m + 1): -                                    if n == 5: -                                        L.append((i, i2, i3, i4, i5)) -                                    else: -                                        for i6 in range(i5, m + 1): -                                            if n == 6: -                                                L.append((i, i2, i3, i4, i5, i6)) -                                            else: -                                                for i7 in range(i6, m + 1): -                                                    if n == 7: -                                                        L.append( -                                                            (i, i2, i3, i4, i5, i6, i7) -                                                        ) -                                                    else: -                                                        for i8 in range(i7, m + 1): -                                                            if n == 8: -                                                                L.append( -                                                                    ( -                                                                        i, -                                                                        i2, -                                                                        i3, -                                                                        i4, -                                                                        i5, -                                                                        i6, -                                                                        i7, -                                                                        i8, -                                                                    ) -                                                                ) -                                                            else: -                                                                for i9 in range( -                                                                    i8, m + 1 -                                                                ): -                                                                    if n == 9: -                                                                        L.append( -                                                                            ( -                                                                                i, -                                                                                i2, -                                                                                i3, -                                                                                i4, -                                                                                i5, -                                                                                i6, -                                                                                i7, -                                                                                i8, -                                                                                i9, -                                                                            ) -                                                                        ) -                                                                    else: -                                                                        for ( -                                                                            i10 -                                                                        ) in range( -                                                                            i9, m + 1 -                                                                        ): -                                                                            L.append( -                                                                                ( -                                                                                    i, -                                                                                    i2, -                                                                                    i3, -                                                                                    i4, -                                                                                    i5, -                                                                                    i6, -                                                                                    i7, -                                                                                    i8, -                                                                                    i9, -                                                                                    i10, -                                                                                ) -                                                                            ) +for i in itertools.combinations_with_replacement(M, n): +    L.append(i)