# -*- coding: utf-8 -*-



# sys.setrecursionlimit(100000)

INF = 2**62-1



def read_int():

    return int(input())





def read_int_n():

    return list(map(int, input().split()))





def read_float():

    return float(input())





def read_float_n():

    return list(map(float, input().split()))





def read_str():

    return input().strip()





def read_str_n():

    return list(map(str, input().split()))





def error_print(*args):

    print(*args, file=sys.stderr)



def slv(N, M, Q, ABCD):



    def dfs(a, n):

        r = 0

        if n == N:

            t = 0

            for abcd in ABCD:

                A, B, C, D = abcd

                if a[B] - a[A] == C:

                    t += D

            return t

        b = a[-1]

        for c in range(b, M+1):

            a.append(c)

            r = max(r, dfs(a, n+1))

            a.pop()

        return r

    return dfs([1], 0)





def main():

    N, M, Q = read_int_n()

    ABCD = [read_int_n() for _ in range(Q)]

    print(slv(N, M, Q, ABCD))





if __name__ == '__main__':

    main()

# -*- coding: utf-8 -*-

from itertools import combinations_with_replacement





def read_int_n():

    return list(map(int, input().split()))





def slv(N, M, Q, ABCD):

    ans = 0

    for a in combinations_with_replacement(list(range(1, M+1)), r=N):

        t = 0

        for abcd in ABCD:

            A, B, C, D = abcd

            if a[B-1] - a[A-1] == C:

                t += D

        ans = max(ans, t)

    return ans





def main():

    N, M, Q = read_int_n()

    ABCD = [read_int_n() for _ in range(Q)]

    print((slv(N, M, Q, ABCD)))





if __name__ == '__main__':

    main()

# -*- coding: utf-8 -*-
# sys.setrecursionlimit(100000)
INF = 2**62 - 1


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def slv(N, M, Q, ABCD):
    def dfs(a, n):
        r = 0
        if n == N:
            t = 0
            for abcd in ABCD:
                A, B, C, D = abcd
                if a[B] - a[A] == C:
                    t += D
            return t
        b = a[-1]
        for c in range(b, M + 1):
            a.append(c)
            r = max(r, dfs(a, n + 1))
            a.pop()
        return r

    return dfs([1], 0)


def main():
    N, M, Q = read_int_n()
    ABCD = [read_int_n() for _ in range(Q)]
    print(slv(N, M, Q, ABCD))


if __name__ == "__main__":
    main()

# -*- coding: utf-8 -*-
from itertools import combinations_with_replacement


def read_int_n():
    return list(map(int, input().split()))


def slv(N, M, Q, ABCD):
    ans = 0
    for a in combinations_with_replacement(list(range(1, M + 1)), r=N):
        t = 0
        for abcd in ABCD:
            A, B, C, D = abcd
            if a[B - 1] - a[A - 1] == C:
                t += D
        ans = max(ans, t)
    return ans


def main():
    N, M, Q = read_int_n()
    ABCD = [read_int_n() for _ in range(Q)]
    print((slv(N, M, Q, ABCD)))


if __name__ == "__main__":
    main()

-# sys.setrecursionlimit(100000) -INF = 2**62 - 1 - - -def read_int(): -    return int(input()) +from itertools import combinations_with_replacement -def read_float(): -    return float(input()) - - -def read_float_n(): -    return list(map(float, input().split())) - - -def read_str(): -    return input().strip() - - -def read_str_n(): -    return list(map(str, input().split())) - - -def error_print(*args): -    print(*args, file=sys.stderr) - - -    def dfs(a, n): -        r = 0 -        if n == N: -            t = 0 -            for abcd in ABCD: -                A, B, C, D = abcd -                if a[B] - a[A] == C: -                    t += D -            return t -        b = a[-1] -        for c in range(b, M + 1): -            a.append(c) -            r = max(r, dfs(a, n + 1)) -            a.pop() -        return r - -    return dfs([1], 0) +    ans = 0 +    for a in combinations_with_replacement(list(range(1, M + 1)), r=N): +        t = 0 +        for abcd in ABCD: +            A, B, C, D = abcd +            if a[B - 1] - a[A - 1] == C: +                t += D +        ans = max(ans, t) +    return ans -    print(slv(N, M, Q, ABCD)) +    print((slv(N, M, Q, ABCD)))