import numpy as np

import itertools



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

ABCD = []

for _ in range(Q):

    ABCD.append(list(map(int,input().split())))



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

MAX = 0

for row in ALL:

    SUM = 0

    for a,b,c,d in ABCD:

        if (row[b-1] - row[a-1]) == c:

            SUM += d



    MAX = max(MAX,SUM)

print(MAX)
import itertools



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

ABCD = []

for _ in range(Q):

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

    ABCD.append(tuple([a-1,b-1,c,d]))





A = list(range(1,M+1))

MAX = 0

for L in itertools.combinations_with_replacement(A,N):

    SUM = 0

    for a,b,c,d in ABCD:

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

            SUM += d



    MAX = max(SUM,MAX)

print(MAX)
import numpy as np
import itertools

N, M, Q = list(map(int, input().split(" ")))
ABCD = []
for _ in range(Q):
    ABCD.append(list(map(int, input().split())))
ALL = list(itertools.combinations_with_replacement(list(range(1, M + 1)), N))
MAX = 0
for row in ALL:
    SUM = 0
    for a, b, c, d in ABCD:
        if (row[b - 1] - row[a - 1]) == c:
            SUM += d
    MAX = max(MAX, SUM)
print(MAX)

import itertools

N, M, Q = list(map(int, input().split()))
ABCD = []
for _ in range(Q):
    a, b, c, d = list(map(int, input().split()))
    ABCD.append(tuple([a - 1, b - 1, c, d]))
A = list(range(1, M + 1))
MAX = 0
for L in itertools.combinations_with_replacement(A, N):
    SUM = 0
    for a, b, c, d in ABCD:
        if L[b] - L[a] == c:
            SUM += d
    MAX = max(SUM, MAX)
print(MAX)

-import numpy as np -N, M, Q = list(map(int, input().split(" "))) +N, M, Q = list(map(int, input().split())) -    ABCD.append(list(map(int, input().split()))) -ALL = list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)) +    a, b, c, d = list(map(int, input().split())) +    ABCD.append(tuple([a - 1, b - 1, c, d])) +A = list(range(1, M + 1)) -for row in ALL: +for L in itertools.combinations_with_replacement(A, N): -        if (row[b - 1] - row[a - 1]) == c: +        if L[b] - L[a] == c: -    MAX = max(MAX, SUM) +    MAX = max(SUM, MAX)