import sys

sys.setrecursionlimit(10**7)



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

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

ans = set()



def generate_arr(target):

    if len(target) == N:

        tmp = 0

        for item in arr:

            if target[item[1]-1] - target[item[0]-1] == item[2]:

                tmp += item[3]

            ans.add(tmp)

        return

    for i in range(target[-1],M+1):

        next_target = target.copy()

        next_target.append(i)

        generate_arr(next_target)

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

    generate_arr([i])

print((max(ans)))
target = []

def dfs(A):

    if len(A) == N:

        tmp = 0

        for a, b, c, d in arr:

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

                tmp += d

        target.append(tmp)

        return

    start = 1

    if len(A) != 0:

        start = A[-1]

    for v in range(start,M+1):

        A.append(v)

        dfs(A)

        A.pop()

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

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

dfs([])

print((max(target)))
import sys

sys.setrecursionlimit(10**7)
N, M, Q = list(map(int, input().split()))
arr = [list(map(int, input().split())) for _ in range(Q)]
ans = set()


def generate_arr(target):
    if len(target) == N:
        tmp = 0
        for item in arr:
            if target[item[1] - 1] - target[item[0] - 1] == item[2]:
                tmp += item[3]
            ans.add(tmp)
        return
    for i in range(target[-1], M + 1):
        next_target = target.copy()
        next_target.append(i)
        generate_arr(next_target)


for i in range(1, M + 1):
    generate_arr([i])
print((max(ans)))

target = []


def dfs(A):
    if len(A) == N:
        tmp = 0
        for a, b, c, d in arr:
            if A[b - 1] - A[a - 1] == c:
                tmp += d
        target.append(tmp)
        return
    start = 1
    if len(A) != 0:
        start = A[-1]
    for v in range(start, M + 1):
        A.append(v)
        dfs(A)
        A.pop()


N, M, Q = list(map(int, input().split()))
arr = [list(map(int, input().split())) for _ in range(Q)]
dfs([])
print((max(target)))

-import sys +target = [] -sys.setrecursionlimit(10**7) + +def dfs(A): +    if len(A) == N: +        tmp = 0 +        for a, b, c, d in arr: +            if A[b - 1] - A[a - 1] == c: +                tmp += d +        target.append(tmp) +        return +    start = 1 +    if len(A) != 0: +        start = A[-1] +    for v in range(start, M + 1): +        A.append(v) +        dfs(A) +        A.pop() + + -ans = set() - - -def generate_arr(target): -    if len(target) == N: -        tmp = 0 -        for item in arr: -            if target[item[1] - 1] - target[item[0] - 1] == item[2]: -                tmp += item[3] -            ans.add(tmp) -        return -    for i in range(target[-1], M + 1): -        next_target = target.copy() -        next_target.append(i) -        generate_arr(next_target) - - -for i in range(1, M + 1): -    generate_arr([i]) -print((max(ans))) +dfs([]) +print((max(target)))