#!/usr/bin/env python3



def main():

    import sys



    def calc(A: list):

        score = 0

        for a, b, c, d in lst:

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

                score += d

        return score



    def dfs(A: list):

        nonlocal ans

        if len(A) == N:

            ans = max(ans, calc(A))

        elif len(A) == 0:

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

                dfs(A + [nx])

        else:

            for nx in range(A[-1], M + 1):

                dfs(A + [nx])



    input = sys.stdin.readline



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

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

    ans = 0

    dfs([])

    print(ans)





main()

#!/usr/bin/env python3

def main():

    import sys

    from itertools import combinations_with_replacement



    input = sys.stdin.readline



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

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



    ans = 0

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

        res = 0

        pattern = list(pattern)

        for a, b, c, d in lst:

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

                res += d

        ans = max(ans, res)



    print(ans)





main()

#!/usr/bin/env python3
def main():
    import sys

    def calc(A: list):
        score = 0
        for a, b, c, d in lst:
            if A[b - 1] - A[a - 1] == c:
                score += d
        return score

    def dfs(A: list):
        nonlocal ans
        if len(A) == N:
            ans = max(ans, calc(A))
        elif len(A) == 0:
            for nx in range(1, M + 1):
                dfs(A + [nx])
        else:
            for nx in range(A[-1], M + 1):
                dfs(A + [nx])

    input = sys.stdin.readline
    N, M, Q = list(map(int, input().split()))
    lst = [list(map(int, input().split())) for _ in range(Q)]
    ans = 0
    dfs([])
    print(ans)


main()

#!/usr/bin/env python3
def main():
    import sys
    from itertools import combinations_with_replacement

    input = sys.stdin.readline
    N, M, Q = list(map(int, input().split()))
    lst = [list(map(int, input().split())) for _ in range(Q)]
    ans = 0
    for pattern in combinations_with_replacement(list(range(1, M + 1)), N):
        res = 0
        pattern = list(pattern)
        for a, b, c, d in lst:
            if pattern[b - 1] - pattern[a - 1] == c:
                res += d
        ans = max(ans, res)
    print(ans)


main()

- -    def calc(A: list): -        score = 0 -        for a, b, c, d in lst: -            if A[b - 1] - A[a - 1] == c: -                score += d -        return score - -    def dfs(A: list): -        nonlocal ans -        if len(A) == N: -            ans = max(ans, calc(A)) -        elif len(A) == 0: -            for nx in range(1, M + 1): -                dfs(A + [nx]) -        else: -            for nx in range(A[-1], M + 1): -                dfs(A + [nx]) +    from itertools import combinations_with_replacement -    dfs([]) +    for pattern in combinations_with_replacement(list(range(1, M + 1)), N): +        res = 0 +        pattern = list(pattern) +        for a, b, c, d in lst: +            if pattern[b - 1] - pattern[a - 1] == c: +                res += d +        ans = max(ans, res)