def main():

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

    Q = []

    for _ in range(q):

        a,b,c,d = list(map(int,input().split()))

        Q.append([a,b,c,d])

    ans = 0

    for a in range(1,11):

        for b in range(a,11):

            for c in range(b,11):

                for d in range(c,11):

                    for e in range(d,11):

                        for f in range(e,11):

                            for g in range(f,11):

                                for h in range(g,11):

                                    for i in range(h,11):

                                        for j in range(i,11):

                                            l = [a,b,c,d,e,f,g,h,i,j]

                                            l = l[0:n]

                                            M = 0

                                            if max(l)>m:

                                                continue

                                            for q in Q:

                                                A,B,C,D = q[0],q[1],q[2],q[3]

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

                                                    M += D

                                            if M>ans:

                                                ans = M

    print(ans)



    



if __name__ == "__main__":

    main()
def main():

    import itertools

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

    Q = []

    for i in range(q):

        Q.append(list(map(int,input().split())))

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

    ans = 0

    for que in itertools.combinations_with_replacement(cmb,n):

        m = 0

        for i in range(q):

            [a,b,c,d] = Q[i]

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

                m += d

        if m>ans:

            ans = m

    print(ans)



if __name__ == "__main__":

    main()

def main():
    n, m, q = list(map(int, input().split()))
    Q = []
    for _ in range(q):
        a, b, c, d = list(map(int, input().split()))
        Q.append([a, b, c, d])
    ans = 0
    for a in range(1, 11):
        for b in range(a, 11):
            for c in range(b, 11):
                for d in range(c, 11):
                    for e in range(d, 11):
                        for f in range(e, 11):
                            for g in range(f, 11):
                                for h in range(g, 11):
                                    for i in range(h, 11):
                                        for j in range(i, 11):
                                            l = [a, b, c, d, e, f, g, h, i, j]
                                            l = l[0:n]
                                            M = 0
                                            if max(l) > m:
                                                continue
                                            for q in Q:
                                                A, B, C, D = q[0], q[1], q[2], q[3]
                                                if l[B - 1] - l[A - 1] == C:
                                                    M += D
                                            if M > ans:
                                                ans = M
    print(ans)


if __name__ == "__main__":
    main()

def main():
    import itertools

    n, m, q = list(map(int, input().split()))
    Q = []
    for i in range(q):
        Q.append(list(map(int, input().split())))
    cmb = [i for i in range(1, m + 1)]
    ans = 0
    for que in itertools.combinations_with_replacement(cmb, n):
        m = 0
        for i in range(q):
            [a, b, c, d] = Q[i]
            if que[b - 1] - que[a - 1] == c:
                m += d
        if m > ans:
            ans = m
    print(ans)


if __name__ == "__main__":
    main()

+    import itertools + -    for _ in range(q): -        a, b, c, d = list(map(int, input().split())) -        Q.append([a, b, c, d]) +    for i in range(q): +        Q.append(list(map(int, input().split()))) +    cmb = [i for i in range(1, m + 1)] -    for a in range(1, 11): -        for b in range(a, 11): -            for c in range(b, 11): -                for d in range(c, 11): -                    for e in range(d, 11): -                        for f in range(e, 11): -                            for g in range(f, 11): -                                for h in range(g, 11): -                                    for i in range(h, 11): -                                        for j in range(i, 11): -                                            l = [a, b, c, d, e, f, g, h, i, j] -                                            l = l[0:n] -                                            M = 0 -                                            if max(l) > m: -                                                continue -                                            for q in Q: -                                                A, B, C, D = q[0], q[1], q[2], q[3] -                                                if l[B - 1] - l[A - 1] == C: -                                                    M += D -                                            if M > ans: -                                                ans = M +    for que in itertools.combinations_with_replacement(cmb, n): +        m = 0 +        for i in range(q): +            [a, b, c, d] = Q[i] +            if que[b - 1] - que[a - 1] == c: +                m += d +        if m > ans: +            ans = m