def main():

    import sys

    input = sys.stdin.buffer.readline

    N, M, Q = (int(i) for i in input().split())

    T = [[int(i) for i in input().split()] for j in range(Q)]



    def dfs(A):

        cur = 0

        if len(A) == N:

            for a, b, c, d in T:

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

                    cur += d

        elif len(A) == 0:

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

                v = dfs(A + [i])

                cur = max(cur, v)

        else:

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

                v = dfs(A + [i])

                cur = max(cur, v)

        return cur



    print((dfs([])))





if __name__ == '__main__':

    main()

def main():

    from itertools import combinations_with_replacement

    import sys

    input = sys.stdin.buffer.readline

    N, M, Q = (int(i) for i in input().split())

    T = [[int(i) for i in input().split()] for j in range(Q)]

    ans = 0

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

        A = sorted(C)

        cur = 0

        for a, b, c, d in T:

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

                cur += d

        ans = max(ans, cur)

    print(ans)





if __name__ == '__main__':

    main()

def main():
    import sys

    input = sys.stdin.buffer.readline
    N, M, Q = (int(i) for i in input().split())
    T = [[int(i) for i in input().split()] for j in range(Q)]

    def dfs(A):
        cur = 0
        if len(A) == N:
            for a, b, c, d in T:
                if A[b - 1] - A[a - 1] == c:
                    cur += d
        elif len(A) == 0:
            for i in range(1, M + 1):
                v = dfs(A + [i])
                cur = max(cur, v)
        else:
            for i in range(A[-1], M + 1):
                v = dfs(A + [i])
                cur = max(cur, v)
        return cur

    print((dfs([])))


if __name__ == "__main__":
    main()

def main():
    from itertools import combinations_with_replacement
    import sys

    input = sys.stdin.buffer.readline
    N, M, Q = (int(i) for i in input().split())
    T = [[int(i) for i in input().split()] for j in range(Q)]
    ans = 0
    for C in combinations_with_replacement(list(range(1, M + 1)), N):
        A = sorted(C)
        cur = 0
        for a, b, c, d in T:
            if A[b - 1] - A[a - 1] == c:
                cur += d
        ans = max(ans, cur)
    print(ans)


if __name__ == "__main__":
    main()

+    from itertools import combinations_with_replacement - -    def dfs(A): +    ans = 0 +    for C in combinations_with_replacement(list(range(1, M + 1)), N): +        A = sorted(C) -        if len(A) == N: -            for a, b, c, d in T: -                if A[b - 1] - A[a - 1] == c: -                    cur += d -        elif len(A) == 0: -            for i in range(1, M + 1): -                v = dfs(A + [i]) -                cur = max(cur, v) -        else: -            for i in range(A[-1], M + 1): -                v = dfs(A + [i]) -                cur = max(cur, v) -        return cur - -    print((dfs([]))) +        for a, b, c, d in T: +            if A[b - 1] - A[a - 1] == c: +                cur += d +        ans = max(ans, cur) +    print(ans)