import itertools

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

max = 0

s = ''

for i in range(M):

    s += str(i)

l = list(itertools.combinations_with_replacement(s, N))

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

for x in l:

    count = 0

    for y in m:

        if int(x[y[1] - 1]) - int(x[y[0] - 1]) == y[2]:

            count += y[3]

    if count > max:

        max = count

print(max)
#cited from maspy

import itertools

import numpy as np



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

A = np.array(list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)))

n = len(A)

score = np.zeros(n, np.int32)

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

for a, b, c, d in m:

    cond = A[:, b - 1] - A[:, a - 1] == c

    score += d * cond

print((score.max()))
import itertools

N, M, Q = list(map(int, input().split()))
max = 0
s = ""
for i in range(M):
    s += str(i)
l = list(itertools.combinations_with_replacement(s, N))
m = [list(map(int, input().split())) for i in range(Q)]
for x in l:
    count = 0
    for y in m:
        if int(x[y[1] - 1]) - int(x[y[0] - 1]) == y[2]:
            count += y[3]
    if count > max:
        max = count
print(max)

# cited from maspy
import itertools
import numpy as np

N, M, Q = list(map(int, input().split()))
A = np.array(list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)))
n = len(A)
score = np.zeros(n, np.int32)
m = [list(map(int, input().split())) for i in range(Q)]
for a, b, c, d in m:
    cond = A[:, b - 1] - A[:, a - 1] == c
    score += d * cond
print((score.max()))

+# cited from maspy +import numpy as np -max = 0 -s = "" -for i in range(M): -    s += str(i) -l = list(itertools.combinations_with_replacement(s, N)) +A = np.array(list(itertools.combinations_with_replacement(list(range(1, M + 1)), N))) +n = len(A) +score = np.zeros(n, np.int32) -for x in l: -    count = 0 -    for y in m: -        if int(x[y[1] - 1]) - int(x[y[0] - 1]) == y[2]: -            count += y[3] -    if count > max: -        max = count -print(max) +for a, b, c, d in m: +    cond = A[:, b - 1] - A[:, a - 1] == c +    score += d * cond +print((score.max()))