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

l = []

for i in range(q):

    l1, l2, l3, l4 = list(map(int, input().split()))

    l.append([l1, l2, l3, l4])

B = []

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

    for b in range(a, m + 1):

        for c in range(b, m + 1):

            for d in range(c, m + 1):

                for e in range(d, m + 1):

                    for f in range(e, m + 1):

                        for g in range(f, m + 1):

                            for h in range(g, m + 1):

                                for i in range(h, m + 1):

                                    for j in range(i, m + 1):

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

                                        A = []

                                        for I in range(n):

                                            A.append(L[I])

                                        C = 0

                                        for k in range(q):

                                            if A[l[k][1] - 1] - A[l[k][0] - 1] == l[k][2]:

                                                C += l[k][3]

                                        B.append(C)

print((max(B)))
def main():

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

    l = []

    for i in range(q):

        l1, l2, l3, l4 = list(map(int, input().split()))

        l.append([l1, l2, l3, l4])

    B = []

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

        for b in range(a, m + 1):

            for c in range(b, m + 1):

                for d in range(c, m + 1):

                    for e in range(d, m + 1):

                        for f in range(e, m + 1):

                            for g in range(f, m + 1):

                                for h in range(g, m + 1):

                                    for i in range(h, m + 1):

                                        for j in range(i, m + 1):

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

                                            A = []

                                            for I in range(n):

                                                A.append(L[I])

                                            C = 0

                                            for k in range(q):

                                                if A[l[k][1] - 1] - A[l[k][0] - 1] == l[k][2]:

                                                    C += l[k][3]

                                            B.append(C)

    print((max(B)))

    

main()
n, m, q = list(map(int, input().split()))
l = []
for i in range(q):
    l1, l2, l3, l4 = list(map(int, input().split()))
    l.append([l1, l2, l3, l4])
B = []
for a in range(1, m + 1):
    for b in range(a, m + 1):
        for c in range(b, m + 1):
            for d in range(c, m + 1):
                for e in range(d, m + 1):
                    for f in range(e, m + 1):
                        for g in range(f, m + 1):
                            for h in range(g, m + 1):
                                for i in range(h, m + 1):
                                    for j in range(i, m + 1):
                                        L = [a, b, c, d, e, f, g, h, i, j]
                                        A = []
                                        for I in range(n):
                                            A.append(L[I])
                                        C = 0
                                        for k in range(q):
                                            if (
                                                A[l[k][1] - 1] - A[l[k][0] - 1]
                                                == l[k][2]
                                            ):
                                                C += l[k][3]
                                        B.append(C)
print((max(B)))

def main():
    n, m, q = list(map(int, input().split()))
    l = []
    for i in range(q):
        l1, l2, l3, l4 = list(map(int, input().split()))
        l.append([l1, l2, l3, l4])
    B = []
    for a in range(1, m + 1):
        for b in range(a, m + 1):
            for c in range(b, m + 1):
                for d in range(c, m + 1):
                    for e in range(d, m + 1):
                        for f in range(e, m + 1):
                            for g in range(f, m + 1):
                                for h in range(g, m + 1):
                                    for i in range(h, m + 1):
                                        for j in range(i, m + 1):
                                            L = [a, b, c, d, e, f, g, h, i, j]
                                            A = []
                                            for I in range(n):
                                                A.append(L[I])
                                            C = 0
                                            for k in range(q):
                                                if (
                                                    A[l[k][1] - 1] - A[l[k][0] - 1]
                                                    == l[k][2]
                                                ):
                                                    C += l[k][3]
                                            B.append(C)
    print((max(B)))


main()

-n, m, q = list(map(int, input().split())) -l = [] -for i in range(q): -    l1, l2, l3, l4 = list(map(int, input().split())) -    l.append([l1, l2, l3, l4]) -B = [] -for a in range(1, m + 1): -    for b in range(a, m + 1): -        for c in range(b, m + 1): -            for d in range(c, m + 1): -                for e in range(d, m + 1): -                    for f in range(e, m + 1): -                        for g in range(f, m + 1): -                            for h in range(g, m + 1): -                                for i in range(h, m + 1): -                                    for j in range(i, m + 1): -                                        L = [a, b, c, d, e, f, g, h, i, j] -                                        A = [] -                                        for I in range(n): -                                            A.append(L[I]) -                                        C = 0 -                                        for k in range(q): -                                            if ( -                                                A[l[k][1] - 1] - A[l[k][0] - 1] -                                                == l[k][2] -                                            ): -                                                C += l[k][3] -                                        B.append(C) -print((max(B))) +def main(): +    n, m, q = list(map(int, input().split())) +    l = [] +    for i in range(q): +        l1, l2, l3, l4 = list(map(int, input().split())) +        l.append([l1, l2, l3, l4]) +    B = [] +    for a in range(1, m + 1): +        for b in range(a, m + 1): +            for c in range(b, m + 1): +                for d in range(c, m + 1): +                    for e in range(d, m + 1): +                        for f in range(e, m + 1): +                            for g in range(f, m + 1): +                                for h in range(g, m + 1): +                                    for i in range(h, m + 1): +                                        for j in range(i, m + 1): +                                            L = [a, b, c, d, e, f, g, h, i, j] +                                            A = [] +                                            for I in range(n): +                                                A.append(L[I]) +                                            C = 0 +                                            for k in range(q): +                                                if ( +                                                    A[l[k][1] - 1] - A[l[k][0] - 1] +                                                    == l[k][2] +                                                ): +                                                    C += l[k][3] +                                            B.append(C) +    print((max(B))) + + +main()