from collections import deque



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

QUERY = [0]*Q

for i in range(Q):

    QUERY[i] = tuple(map(int,input().split()))



ans = 0

D = deque()

D.append((1,[1]))

dlen = 1

while dlen != 0:

    Alen,A = D.pop()

    dlen -= 1

    if Alen == N:

        e = 0

        for a,b,c,d in QUERY:

            a -= 1

            b -= 1

            if A[b] - A[a] == c:

                e += d

        ans = max(ans,e)

    else:

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

            D.append((Alen+1,A+[AA]))

            dlen += 1



print(ans)

from collections import deque



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

QUERY = [0]*Q

for i in range(Q):

    QUERY[i] = tuple(map(int,input().split()))



ans = 0

D = deque()

D.append(-1)

D.append(1)

dlen = 2

A = deque()

Alen = 0

while dlen != 0:

    f = D.pop()

    dlen -= 1

    if f == -1:

        A.pop()

        Alen -= 1

        continue

    A.append(f)

    Alen += 1

    if Alen == N:

        e = 0

        for a,b,c,d in QUERY:

            a -= 1

            b -= 1

            if A[b] - A[a] == c:

                e += d

        ans = max(ans,e)

    else:

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

            D.append(-1)

            D.append(AA)

            dlen += 2



print(ans)

from collections import deque

N, M, Q = list(map(int, input().split()))
QUERY = [0] * Q
for i in range(Q):
    QUERY[i] = tuple(map(int, input().split()))
ans = 0
D = deque()
D.append((1, [1]))
dlen = 1
while dlen != 0:
    Alen, A = D.pop()
    dlen -= 1
    if Alen == N:
        e = 0
        for a, b, c, d in QUERY:
            a -= 1
            b -= 1
            if A[b] - A[a] == c:
                e += d
        ans = max(ans, e)
    else:
        for AA in range(A[-1], M + 1):
            D.append((Alen + 1, A + [AA]))
            dlen += 1
print(ans)

from collections import deque

N, M, Q = list(map(int, input().split()))
QUERY = [0] * Q
for i in range(Q):
    QUERY[i] = tuple(map(int, input().split()))
ans = 0
D = deque()
D.append(-1)
D.append(1)
dlen = 2
A = deque()
Alen = 0
while dlen != 0:
    f = D.pop()
    dlen -= 1
    if f == -1:
        A.pop()
        Alen -= 1
        continue
    A.append(f)
    Alen += 1
    if Alen == N:
        e = 0
        for a, b, c, d in QUERY:
            a -= 1
            b -= 1
            if A[b] - A[a] == c:
                e += d
        ans = max(ans, e)
    else:
        for AA in range(A[-1], M + 1):
            D.append(-1)
            D.append(AA)
            dlen += 2
print(ans)

-D.append((1, [1])) -dlen = 1 +D.append(-1) +D.append(1) +dlen = 2 +A = deque() +Alen = 0 -    Alen, A = D.pop() +    f = D.pop() +    if f == -1: +        A.pop() +        Alen -= 1 +        continue +    A.append(f) +    Alen += 1 -            D.append((Alen + 1, A + [AA])) -            dlen += 1 +            D.append(-1) +            D.append(AA) +            dlen += 2