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

a = [0]*Q

b = [0]*Q

c = [0]*Q

d = [0]*Q

for i in range(Q):

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



lun = [0]*N

l = []



def make(n):

    if len(n) > N:

        return 0

    if int(n) > 3234566667:

        return 0

    else:



        p = []

        for i in range(int(n[-1]),M):

            p.append(str(i))



        lun.append(n)

        for i in p:

            lun.append(n)

            if len(n) == N:

                l.append(n)

            make(n+i)

        return 0

for i in range(M):

    make(str(i))



l = list(set(l))



nax = 0

for x in l:

    score = 0

    for i in range(Q):

        if int(x[b[i]-1]) - int(x[a[i]-1]) == c[i]:

            score += d[i]

    nax = max(nax,score)



print(nax)
import itertools



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

a = [0]*Q

b = [0]*Q

c = [0]*Q

d = [0]*Q

for i in range(Q):

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



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



nax = 0

for x in l:

    score = 0

    for i in range(Q):

        if int(x[b[i]-1]) - int(x[a[i]-1]) == c[i]:

            score += d[i]

    nax = max(nax,score)



print(nax)
N, M, Q = list(map(int, (input().split())))
a = [0] * Q
b = [0] * Q
c = [0] * Q
d = [0] * Q
for i in range(Q):
    a[i], b[i], c[i], d[i] = list(map(int, (input().split())))
lun = [0] * N
l = []


def make(n):
    if len(n) > N:
        return 0
    if int(n) > 3234566667:
        return 0
    else:
        p = []
        for i in range(int(n[-1]), M):
            p.append(str(i))
        lun.append(n)
        for i in p:
            lun.append(n)
            if len(n) == N:
                l.append(n)
            make(n + i)
        return 0


for i in range(M):
    make(str(i))
l = list(set(l))
nax = 0
for x in l:
    score = 0
    for i in range(Q):
        if int(x[b[i] - 1]) - int(x[a[i] - 1]) == c[i]:
            score += d[i]
    nax = max(nax, score)
print(nax)

import itertools

N, M, Q = list(map(int, (input().split())))
a = [0] * Q
b = [0] * Q
c = [0] * Q
d = [0] * Q
for i in range(Q):
    a[i], b[i], c[i], d[i] = list(map(int, (input().split())))
l = list(itertools.combinations_with_replacement(list(range(1, M + 1)), N))
nax = 0
for x in l:
    score = 0
    for i in range(Q):
        if int(x[b[i] - 1]) - int(x[a[i] - 1]) == c[i]:
            score += d[i]
    nax = max(nax, score)
print(nax)

+import itertools + -lun = [0] * N -l = [] - - -def make(n): -    if len(n) > N: -        return 0 -    if int(n) > 3234566667: -        return 0 -    else: -        p = [] -        for i in range(int(n[-1]), M): -            p.append(str(i)) -        lun.append(n) -        for i in p: -            lun.append(n) -            if len(n) == N: -                l.append(n) -            make(n + i) -        return 0 - - -for i in range(M): -    make(str(i)) -l = list(set(l)) +l = list(itertools.combinations_with_replacement(list(range(1, M + 1)), N))