import sys

input = sys.stdin.readline

sys.setrecursionlimit(2 * 10**6)





def inpl():

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





back = []





def backtrack(n, ml, mh, tmp=[]):

    if n == 0:

        back.append(tmp)

        return



    for i in range(ml, mh + 1):

        backtrack(n - 1, i, mh, tmp + [i])





def main():

    def count(carrer):

        ret = 0

        for a, b, c, d in abcd:

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

                ret += d

        return ret

    N, M, Q = inpl()

    ans = 0

    abcd = [inpl() for _ in range(Q)]

    backtrack(N, 1, M)

    for carrer in back:

        # print(carrer)

        tmp = count(carrer)

        if tmp > ans:

            ans = tmp

    print(ans)





if __name__ == '__main__':

    main()

import sys

input = sys.stdin.readline

sys.setrecursionlimit(2 * 10**6)





def inpl():

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





def backtrack(n, ml, mh, tmp=[]):

    if n == 0:

        yield tmp

        return



    for i in range(ml, mh + 1):

        yield from backtrack(n - 1, i, mh, tmp + [i])





def main():

    def count(ca):

        return sum(d for a, b, c, d in abcd if ca[b - 1] - ca[a - 1] == c)

    N, M, Q = inpl()

    abcd = [inpl() for _ in range(Q)]

    print((max(count(c) for c in backtrack(N, 1, M))))





if __name__ == '__main__':

    main()

import sys

input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


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


back = []


def backtrack(n, ml, mh, tmp=[]):
    if n == 0:
        back.append(tmp)
        return
    for i in range(ml, mh + 1):
        backtrack(n - 1, i, mh, tmp + [i])


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

    N, M, Q = inpl()
    ans = 0
    abcd = [inpl() for _ in range(Q)]
    backtrack(N, 1, M)
    for carrer in back:
        # print(carrer)
        tmp = count(carrer)
        if tmp > ans:
            ans = tmp
    print(ans)


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


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


def backtrack(n, ml, mh, tmp=[]):
    if n == 0:
        yield tmp
        return
    for i in range(ml, mh + 1):
        yield from backtrack(n - 1, i, mh, tmp + [i])


def main():
    def count(ca):
        return sum(d for a, b, c, d in abcd if ca[b - 1] - ca[a - 1] == c)

    N, M, Q = inpl()
    abcd = [inpl() for _ in range(Q)]
    print((max(count(c) for c in backtrack(N, 1, M))))


if __name__ == "__main__":
    main()

-back = [] - - -        back.append(tmp) +        yield tmp -        backtrack(n - 1, i, mh, tmp + [i]) +        yield from backtrack(n - 1, i, mh, tmp + [i]) -    def count(carrer): -        ret = 0 -        for a, b, c, d in abcd: -            if carrer[b - 1] - carrer[a - 1] == c: -                ret += d -        return ret +    def count(ca): +        return sum(d for a, b, c, d in abcd if ca[b - 1] - ca[a - 1] == c) -    ans = 0 -    backtrack(N, 1, M) -    for carrer in back: -        # print(carrer) -        tmp = count(carrer) -        if tmp > ans: -            ans = tmp -    print(ans) +    print((max(count(c) for c in backtrack(N, 1, M))))