from itertools import combinations_with_replacement as cwd



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

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

ABCD = []



for i in range(Q):

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

    a, b = a - 1, b - 1

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



res = 0

for i in A:

    tmp = 0

    for j in ABCD:

        if i[j[1]] - i[j[0]] == j[2]:

            tmp += j[3]

    res = max(res, tmp)



print(res)

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

res = 0



def dfs(L: list):

    global res

    A = L[::]

    if len(A) == N + 1:

        now = 0

        for i in range(Q):

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

                now += d[i]

        res = max(res, now)

        # print(A, now)

        return

    

    A.append(A[-1])

    while A[-1] <= M:

        dfs(A)

        A[-1] += 1

    

a, b, c, d = [], [], [], []

for _ in range(Q):

    _a, _b, _c, _d = list(map(int, input().split()))

    a.append(_a)

    b.append(_b)

    c.append(_c)

    d.append(_d)



dfs([1])

print(res)

from itertools import combinations_with_replacement as cwd

N, M, Q = list(map(int, input().split()))
A = list(cwd(list(range(1, M + 1)), N))
ABCD = []
for i in range(Q):
    a, b, c, d = list(map(int, input().split()))
    a, b = a - 1, b - 1
    ABCD.append([a, b, c, d])
res = 0
for i in A:
    tmp = 0
    for j in ABCD:
        if i[j[1]] - i[j[0]] == j[2]:
            tmp += j[3]
    res = max(res, tmp)
print(res)

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


def dfs(L: list):
    global res
    A = L[::]
    if len(A) == N + 1:
        now = 0
        for i in range(Q):
            if A[b[i]] - A[a[i]] == c[i]:
                now += d[i]
        res = max(res, now)
        # print(A, now)
        return
    A.append(A[-1])
    while A[-1] <= M:
        dfs(A)
        A[-1] += 1


a, b, c, d = [], [], [], []
for _ in range(Q):
    _a, _b, _c, _d = list(map(int, input().split()))
    a.append(_a)
    b.append(_b)
    c.append(_c)
    d.append(_d)
dfs([1])
print(res)

-from itertools import combinations_with_replacement as cwd +N, M, Q = list(map(int, input().split())) +res = 0 -N, M, Q = list(map(int, input().split())) -A = list(cwd(list(range(1, M + 1)), N)) -ABCD = [] -for i in range(Q): -    a, b, c, d = list(map(int, input().split())) -    a, b = a - 1, b - 1 -    ABCD.append([a, b, c, d]) -res = 0 -for i in A: -    tmp = 0 -    for j in ABCD: -        if i[j[1]] - i[j[0]] == j[2]: -            tmp += j[3] -    res = max(res, tmp) + +def dfs(L: list): +    global res +    A = L[::] +    if len(A) == N + 1: +        now = 0 +        for i in range(Q): +            if A[b[i]] - A[a[i]] == c[i]: +                now += d[i] +        res = max(res, now) +        # print(A, now) +        return +    A.append(A[-1]) +    while A[-1] <= M: +        dfs(A) +        A[-1] += 1 + + +a, b, c, d = [], [], [], [] +for _ in range(Q): +    _a, _b, _c, _d = list(map(int, input().split())) +    a.append(_a) +    b.append(_b) +    c.append(_c) +    d.append(_d) +dfs([1])