def comb(k, s, r):

    if k == n:

        r.append(s[1:])

        return

    for ai in range(s[-1], m + 1):

        comb(k + 1, s + [ai], r)

    return r



def main():

    abcd = [[int(i) for i in input().split()] for _ in range(q)]

    s = 0

    for p in comb(0, [1], []):

        t = 0

        for a, b, c, d in abcd:

            t += d * (p[b - 1] - p[a - 1] == c)

        s = max(s, t)

    print(s)



if __name__ == '__main__':

    n, m, q = list(map(int, input().split()))

    main()
def main(k, s):

    if k == n:

        return sum(d * (s[b] - s[a] == c) for a, b, c, d in abcd)

    return max(main(k + 1, s + [ai]) for ai in range(s[-1], m + 1))



if __name__ == '__main__':

    n, m, q = list(map(int, input().split()))

    abcd = [[int(i) for i in input().split()] for _ in range(q)]

    print((main(0, [1])))
def comb(k, s, r):
    if k == n:
        r.append(s[1:])
        return
    for ai in range(s[-1], m + 1):
        comb(k + 1, s + [ai], r)
    return r


def main():
    abcd = [[int(i) for i in input().split()] for _ in range(q)]
    s = 0
    for p in comb(0, [1], []):
        t = 0
        for a, b, c, d in abcd:
            t += d * (p[b - 1] - p[a - 1] == c)
        s = max(s, t)
    print(s)


if __name__ == "__main__":
    n, m, q = list(map(int, input().split()))
    main()

def main(k, s):
    if k == n:
        return sum(d * (s[b] - s[a] == c) for a, b, c, d in abcd)
    return max(main(k + 1, s + [ai]) for ai in range(s[-1], m + 1))


if __name__ == "__main__":
    n, m, q = list(map(int, input().split()))
    abcd = [[int(i) for i in input().split()] for _ in range(q)]
    print((main(0, [1])))

-def comb(k, s, r): +def main(k, s): -        r.append(s[1:]) -        return -    for ai in range(s[-1], m + 1): -        comb(k + 1, s + [ai], r) -    return r - - -def main(): -    abcd = [[int(i) for i in input().split()] for _ in range(q)] -    s = 0 -    for p in comb(0, [1], []): -        t = 0 -        for a, b, c, d in abcd: -            t += d * (p[b - 1] - p[a - 1] == c) -        s = max(s, t) -    print(s) +        return sum(d * (s[b] - s[a] == c) for a, b, c, d in abcd) +    return max(main(k + 1, s + [ai]) for ai in range(s[-1], m + 1)) -    main() +    abcd = [[int(i) for i in input().split()] for _ in range(q)] +    print((main(0, [1])))