import itertools



N, M, Q = list(map(int, input().split()))

l = [list(map(int, input().split())) for _ in range(Q)]

n = [i+1 for i in range(M)]

A_list = list(itertools.combinations_with_replacement(n,N))

ans = 0

for A in A_list:

    ans_ = 0

    for a, b, c, d in l:

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

            ans_ += d

        ans = max(ans, ans_)



print(ans)
N, M, Q = list(map(int, input().split()))

l = [list(map(int, input().split())) for _ in range(Q)]

A = [1] * N

ans = 0



for a, b, c, d in l:

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

        ans += d



if M > 1:

    while True:

        A[-1] += 1

        if A[-1] == M + 1:

            for i in range(1, N):

                A[-(i+1)] += 1

                for k in range(N-(i+1), N):

                    A[k] = A[-(i+1)]

                if A[-(i+1)] != M + 1:

                    break

            

        ans_ = 0

        for a, b, c, d in l:

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

                ans_ += d

        ans = max(ans, ans_)



        if A[0] == M:

            break

print(ans)



    

    









import itertools

N, M, Q = list(map(int, input().split()))
l = [list(map(int, input().split())) for _ in range(Q)]
n = [i + 1 for i in range(M)]
A_list = list(itertools.combinations_with_replacement(n, N))
ans = 0
for A in A_list:
    ans_ = 0
    for a, b, c, d in l:
        if A[b - 1] - A[a - 1] == c:
            ans_ += d
        ans = max(ans, ans_)
print(ans)

N, M, Q = list(map(int, input().split()))
l = [list(map(int, input().split())) for _ in range(Q)]
A = [1] * N
ans = 0
for a, b, c, d in l:
    if A[b - 1] - A[a - 1] == c:
        ans += d
if M > 1:
    while True:
        A[-1] += 1
        if A[-1] == M + 1:
            for i in range(1, N):
                A[-(i + 1)] += 1
                for k in range(N - (i + 1), N):
                    A[k] = A[-(i + 1)]
                if A[-(i + 1)] != M + 1:
                    break
        ans_ = 0
        for a, b, c, d in l:
            if A[b - 1] - A[a - 1] == c:
                ans_ += d
        ans = max(ans, ans_)
        if A[0] == M:
            break
print(ans)

-import itertools - -n = [i + 1 for i in range(M)] -A_list = list(itertools.combinations_with_replacement(n, N)) +A = [1] * N -for A in A_list: -    ans_ = 0 -    for a, b, c, d in l: -        if A[b - 1] - A[a - 1] == c: -            ans_ += d +for a, b, c, d in l: +    if A[b - 1] - A[a - 1] == c: +        ans += d +if M > 1: +    while True: +        A[-1] += 1 +        if A[-1] == M + 1: +            for i in range(1, N): +                A[-(i + 1)] += 1 +                for k in range(N - (i + 1), N): +                    A[k] = A[-(i + 1)] +                if A[-(i + 1)] != M + 1: +                    break +        ans_ = 0 +        for a, b, c, d in l: +            if A[b - 1] - A[a - 1] == c: +                ans_ += d +        if A[0] == M: +            break