n, m, q = 0, 0, 0

ABCD = []

ans = 0





def dfs(ns):

    global ans



    A = [int(c) for c in ns.split()]

    if len(A) == n:

        s = 0

        for qi in range(q):

            a, b, c, d = ABCD[qi]

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

                s += d



        ans = max(s, ans)

        return



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

        dfs('{} {}'.format(ns, str(i)))





def main():

    global n, m, q, ABCD

    n, m, q = [int(e) for e in input().split()]

    ABCD = [[int(e) for e in input().split()] for i in range(q)]



    for mi in range(1, m + 1):

        dfs(str(mi))



    print(ans)





if __name__ == '__main__':

    main()

n, m, q = 0, 0, 0

ABCD = []

ans = 0





def dfs(A):

    global ans



    if len(A) == n:

        s = 0

        for qi in range(q):

            a, b, c, d = ABCD[qi]

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

                s += d



        ans = max(s, ans)

        return



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

        AC = A.copy()

        AC.append(i)

        dfs(AC)





def main():

    global n, m, q, ABCD

    n, m, q = [int(e) for e in input().split()]

    ABCD = [[int(e) for e in input().split()] for i in range(q)]



    for mi in range(1, m + 1):

        dfs([mi])



    print(ans)





if __name__ == '__main__':

    main()

n, m, q = 0, 0, 0
ABCD = []
ans = 0


def dfs(ns):
    global ans
    A = [int(c) for c in ns.split()]
    if len(A) == n:
        s = 0
        for qi in range(q):
            a, b, c, d = ABCD[qi]
            if A[b - 1] - A[a - 1] == c:
                s += d
        ans = max(s, ans)
        return
    for i in range(A[-1], m + 1):
        dfs("{} {}".format(ns, str(i)))


def main():
    global n, m, q, ABCD
    n, m, q = [int(e) for e in input().split()]
    ABCD = [[int(e) for e in input().split()] for i in range(q)]
    for mi in range(1, m + 1):
        dfs(str(mi))
    print(ans)


if __name__ == "__main__":
    main()

n, m, q = 0, 0, 0
ABCD = []
ans = 0


def dfs(A):
    global ans
    if len(A) == n:
        s = 0
        for qi in range(q):
            a, b, c, d = ABCD[qi]
            if A[b - 1] - A[a - 1] == c:
                s += d
        ans = max(s, ans)
        return
    for i in range(A[-1], m + 1):
        AC = A.copy()
        AC.append(i)
        dfs(AC)


def main():
    global n, m, q, ABCD
    n, m, q = [int(e) for e in input().split()]
    ABCD = [[int(e) for e in input().split()] for i in range(q)]
    for mi in range(1, m + 1):
        dfs([mi])
    print(ans)


if __name__ == "__main__":
    main()

-def dfs(ns): +def dfs(A): -    A = [int(c) for c in ns.split()] -        dfs("{} {}".format(ns, str(i))) +        AC = A.copy() +        AC.append(i) +        dfs(AC) -        dfs(str(mi)) +        dfs([mi])