import sys

sys.setrecursionlimit(10 ** 6)





def dfs(num):

    global N, ans, M

    n = len(num)

    if n == N:

        cnt = 0

        for a, b, c, d in con:

            a -= 1; b -= 1

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

                cnt += d

        ans = max(ans, cnt)

    else:

        e = 1 if n == 0 else num[-1]

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

            dfs((*num, i))





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

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



ans = 0

dfs(tuple())



print(ans)

def dfs(i, p, seq):

    global N, M

    ret = 0

    if i == N:

        ret = check(seq)

    else:

        for x in range(p, M + 1):

            ret = max(ret, dfs(i + 1, x, tuple(list(seq) + [x])))

    return ret





def check(seq):

    cnt = 0

    for a, b, c, d in query:

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

            cnt += d

    return cnt





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

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



print((dfs(0, 1, tuple())))

import sys

sys.setrecursionlimit(10**6)


def dfs(num):
    global N, ans, M
    n = len(num)
    if n == N:
        cnt = 0
        for a, b, c, d in con:
            a -= 1
            b -= 1
            if num[b] - num[a] == c:
                cnt += d
        ans = max(ans, cnt)
    else:
        e = 1 if n == 0 else num[-1]
        for i in range(e, M + 1):
            dfs((*num, i))


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

def dfs(i, p, seq):
    global N, M
    ret = 0
    if i == N:
        ret = check(seq)
    else:
        for x in range(p, M + 1):
            ret = max(ret, dfs(i + 1, x, tuple(list(seq) + [x])))
    return ret


def check(seq):
    cnt = 0
    for a, b, c, d in query:
        if seq[b - 1] - seq[a - 1] == c:
            cnt += d
    return cnt


N, M, Q = list(map(int, input().split()))
query = [tuple(map(int, input().split())) for _ in range(Q)]
print((dfs(0, 1, tuple())))

-import sys - -sys.setrecursionlimit(10**6) +def dfs(i, p, seq): +    global N, M +    ret = 0 +    if i == N: +        ret = check(seq) +    else: +        for x in range(p, M + 1): +            ret = max(ret, dfs(i + 1, x, tuple(list(seq) + [x]))) +    return ret -def dfs(num): -    global N, ans, M -    n = len(num) -    if n == N: -        cnt = 0 -        for a, b, c, d in con: -            a -= 1 -            b -= 1 -            if num[b] - num[a] == c: -                cnt += d -        ans = max(ans, cnt) -    else: -        e = 1 if n == 0 else num[-1] -        for i in range(e, M + 1): -            dfs((*num, i)) +def check(seq): +    cnt = 0 +    for a, b, c, d in query: +        if seq[b - 1] - seq[a - 1] == c: +            cnt += d +    return cnt -con = [tuple(map(int, input().split())) for _ in range(Q)] -ans = 0 -dfs(tuple()) -print(ans) +query = [tuple(map(int, input().split())) for _ in range(Q)] +print((dfs(0, 1, tuple())))