from itertools import combinations_with_replacement



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

A, B, C, D = [], [], [], []

for _ in range(Q):

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

    A.append(a - 1)

    B.append(b - 1)

    C.append(c)

    D.append(d)

res = 0

for p in combinations_with_replacement(list(range(1, M + 1)), N):

    cur = 0

    for a, b, c, d in zip(A, B, C, D):

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

            cur += d

    res = max(res, cur)

print(res)
def dfs(n, lower):

    if n == N:

        return [[]]

    else:

        res = []

        for i in range(lower, M + 1):

            for p in dfs(n + 1, i):

                res.append([i] + p)

        return res





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

A, B, C, D = [], [], [], []

for _ in range(Q):

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

    A.append(a - 1)

    B.append(b - 1)

    C.append(c)

    D.append(d)

res = 0

for p in dfs(0, 1):

    cur = 0

    for a, b, c, d in zip(A, B, C, D):

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

            cur += d

    res = max(res, cur)

print(res)
from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))
A, B, C, D = [], [], [], []
for _ in range(Q):
    a, b, c, d = list(map(int, input().split()))
    A.append(a - 1)
    B.append(b - 1)
    C.append(c)
    D.append(d)
res = 0
for p in combinations_with_replacement(list(range(1, M + 1)), N):
    cur = 0
    for a, b, c, d in zip(A, B, C, D):
        if p[b] - p[a] == c:
            cur += d
    res = max(res, cur)
print(res)

def dfs(n, lower):
    if n == N:
        return [[]]
    else:
        res = []
        for i in range(lower, M + 1):
            for p in dfs(n + 1, i):
                res.append([i] + p)
        return res


N, M, Q = list(map(int, input().split()))
A, B, C, D = [], [], [], []
for _ in range(Q):
    a, b, c, d = list(map(int, input().split()))
    A.append(a - 1)
    B.append(b - 1)
    C.append(c)
    D.append(d)
res = 0
for p in dfs(0, 1):
    cur = 0
    for a, b, c, d in zip(A, B, C, D):
        if p[b] - p[a] == c:
            cur += d
    res = max(res, cur)
print(res)

-from itertools import combinations_with_replacement +def dfs(n, lower): +    if n == N: +        return [[]] +    else: +        res = [] +        for i in range(lower, M + 1): +            for p in dfs(n + 1, i): +                res.append([i] + p) +        return res + -for p in combinations_with_replacement(list(range(1, M + 1)), N): +for p in dfs(0, 1):