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

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



def calc(p):

    ret = 0

    # 各条件を満たしていれば、dを加算していく

    for a, b, c, d in abcd:

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

            ret += d

    return ret



ans = [0]

def dfs(s):

    if len(s) == N:

        ans[0] = max(ans[0], calc(s))

        return

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

        dfs(s+[i])



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

    dfs([i])



print((ans[0]))



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

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

ans = 0

def calc(p):

    ret = 0

    for a, b, c, d in abcd:

        a, b = a-1, b-1

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

            ret += d

    return ret



def dfs(a, d):

    global ans

    if d == N:

        ans = max(ans, calc(a))

        return

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

        dfs(a+[i], d+1)



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

    dfs([i], 1)



print(ans)

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


def calc(p):
    ret = 0
    # 各条件を満たしていれば、dを加算していく
    for a, b, c, d in abcd:
        if p[b - 1] - p[a - 1] == c:
            ret += d
    return ret


ans = [0]


def dfs(s):
    if len(s) == N:
        ans[0] = max(ans[0], calc(s))
        return
    for i in range(s[-1], M + 1):
        dfs(s + [i])


for i in range(1, M + 1):
    dfs([i])
print((ans[0]))

N, M, Q = list(map(int, input().split()))
abcd = [tuple(map(int, input().split())) for _ in range(Q)]
ans = 0


def calc(p):
    ret = 0
    for a, b, c, d in abcd:
        a, b = a - 1, b - 1
        if p[b] - p[a] == c:
            ret += d
    return ret


def dfs(a, d):
    global ans
    if d == N:
        ans = max(ans, calc(a))
        return
    for i in range(a[-1], M + 1):
        dfs(a + [i], d + 1)


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

-abcd = [list(map(int, input().split())) for _ in range(Q)] +abcd = [tuple(map(int, input().split())) for _ in range(Q)] +ans = 0 -    # 各条件を満たしていれば、dを加算していく -        if p[b - 1] - p[a - 1] == c: +        a, b = a - 1, b - 1 +        if p[b] - p[a] == c: -ans = [0] - - -def dfs(s): -    if len(s) == N: -        ans[0] = max(ans[0], calc(s)) +def dfs(a, d): +    global ans +    if d == N: +        ans = max(ans, calc(a)) -    for i in range(s[-1], M + 1): -        dfs(s + [i]) +    for i in range(a[-1], M + 1): +        dfs(a + [i], d + 1) -    dfs([i]) -print((ans[0])) +    dfs([i], 1) +print(ans)