from itertools import combinations_with_replacement

def main():

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

    qlist = []

    for _ in range(q):

        qlist.append(tuple(map(int, input().split())))



    nums = [i for i in range(1, m + 1)]

    alist = tuple(combinations_with_replacement(nums, n))



    r = 0

    for al in alist:

        tr = 0

        for eq in qlist:

            eq0 = eq[0] - 1

            eq1 = eq[1] - 1

            eq2 = eq[2]

            eq3 = eq[3]

            if al[eq1] - al[eq0] == eq2:

                tr += eq3

        r = max(r, tr)



    print(r)



if __name__ == '__main__':

    main()

import sys

read = sys.stdin.read

readlines = sys.stdin.readlines

from itertools import combinations_with_replacement

def main():

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

    abcd = []

    for _ in range(q):

        q = tuple(map(int, input().split()))

        abcd.append(q)

    c1 = tuple(combinations_with_replacement(list(range(1,m+1)), n))

    r = 0

    for ce in c1:

        t = 0

        for qe in abcd:

            a, b, c, d = qe

            a -= 1

            b -= 1

            if ce[b] - ce[a] == c:

                t += d

        r = max(r, t)

    print(r)



if __name__ == '__main__':

    main()
from itertools import combinations_with_replacement


def main():
    n, m, q = list(map(int, input().split()))
    qlist = []
    for _ in range(q):
        qlist.append(tuple(map(int, input().split())))
    nums = [i for i in range(1, m + 1)]
    alist = tuple(combinations_with_replacement(nums, n))
    r = 0
    for al in alist:
        tr = 0
        for eq in qlist:
            eq0 = eq[0] - 1
            eq1 = eq[1] - 1
            eq2 = eq[2]
            eq3 = eq[3]
            if al[eq1] - al[eq0] == eq2:
                tr += eq3
        r = max(r, tr)
    print(r)


if __name__ == "__main__":
    main()

import sys

read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import combinations_with_replacement


def main():
    n, m, q = list(map(int, input().split()))
    abcd = []
    for _ in range(q):
        q = tuple(map(int, input().split()))
        abcd.append(q)
    c1 = tuple(combinations_with_replacement(list(range(1, m + 1)), n))
    r = 0
    for ce in c1:
        t = 0
        for qe in abcd:
            a, b, c, d = qe
            a -= 1
            b -= 1
            if ce[b] - ce[a] == c:
                t += d
        r = max(r, t)
    print(r)


if __name__ == "__main__":
    main()

+import sys + +read = sys.stdin.read +readlines = sys.stdin.readlines -    qlist = [] +    abcd = [] -        qlist.append(tuple(map(int, input().split()))) -    nums = [i for i in range(1, m + 1)] -    alist = tuple(combinations_with_replacement(nums, n)) +        q = tuple(map(int, input().split())) +        abcd.append(q) +    c1 = tuple(combinations_with_replacement(list(range(1, m + 1)), n)) -    for al in alist: -        tr = 0 -        for eq in qlist: -            eq0 = eq[0] - 1 -            eq1 = eq[1] - 1 -            eq2 = eq[2] -            eq3 = eq[3] -            if al[eq1] - al[eq0] == eq2: -                tr += eq3 -        r = max(r, tr) +    for ce in c1: +        t = 0 +        for qe in abcd: +            a, b, c, d = qe +            a -= 1 +            b -= 1 +            if ce[b] - ce[a] == c: +                t += d +        r = max(r, t)