import math

#X=int(input())

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

S=[[]for i in range(Q)]

for i in range(Q):

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

    S[i]=a

ans=0

T=0

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):

                                        D=[a,b,c,d,e,f,g,h,i,j]

                                        for k in range(Q):

                                            if D[S[k][1]-1]-D[S[k][0]-1]==S[k][2]:

                                                ans+=S[k][3]

                                        if ans>=T:

                                            T=ans

                                        ans = 0

print(T)

import math

import sys

sys.setrecursionlimit(10**7)

#X=int(input())

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

S=[[]for i in range(Q)]

for i in range(Q):

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

    S[i]=a

ans=0

T=0

def solve(l):

    global ans

    if len(l)>=N:

        tmp=0

        for k in range(Q):

            if l[S[k][1]-1]-l[S[k][0]-1]==S[k][2]:

                tmp+=S[k][3]

        ans=max(ans,tmp)

        return

    for i in range(l[-1],M+1):

        solve(l+[i])

solve([1])

print(ans)
import math

# X=int(input())
N, M, Q = list(map(int, input().split()))
S = [[] for i in range(Q)]
for i in range(Q):
    a = list(map(int, input().split()))
    S[i] = a
ans = 0
T = 0
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):
                                        D = [a, b, c, d, e, f, g, h, i, j]
                                        for k in range(Q):
                                            if (
                                                D[S[k][1] - 1] - D[S[k][0] - 1]
                                                == S[k][2]
                                            ):
                                                ans += S[k][3]
                                        if ans >= T:
                                            T = ans
                                        ans = 0
print(T)

import math
import sys

sys.setrecursionlimit(10**7)
# X=int(input())
N, M, Q = list(map(int, input().split()))
S = [[] for i in range(Q)]
for i in range(Q):
    a = list(map(int, input().split()))
    S[i] = a
ans = 0
T = 0


def solve(l):
    global ans
    if len(l) >= N:
        tmp = 0
        for k in range(Q):
            if l[S[k][1] - 1] - l[S[k][0] - 1] == S[k][2]:
                tmp += S[k][3]
        ans = max(ans, tmp)
        return
    for i in range(l[-1], M + 1):
        solve(l + [i])


solve([1])
print(ans)

+import sys +sys.setrecursionlimit(10**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): -                            for h in range(g, M + 1): -                                for i in range(h, M + 1): -                                    for j in range(i, M + 1): -                                        D = [a, b, c, d, e, f, g, h, i, j] -                                        for k in range(Q): -                                            if ( -                                                D[S[k][1] - 1] - D[S[k][0] - 1] -                                                == S[k][2] -                                            ): -                                                ans += S[k][3] -                                        if ans >= T: -                                            T = ans -                                        ans = 0 -print(T) + + +def solve(l): +    global ans +    if len(l) >= N: +        tmp = 0 +        for k in range(Q): +            if l[S[k][1] - 1] - l[S[k][0] - 1] == S[k][2]: +                tmp += S[k][3] +        ans = max(ans, tmp) +        return +    for i in range(l[-1], M + 1): +        solve(l + [i]) + + +solve([1]) +print(ans)