import itertools

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

abcd = [list(map(int,input().split())) for i in range(q)]



ans = 0

for iter in itertools.combinations_with_replacement(list(range(1,m+1)),n):

    result = 0

    for i in range(q):

        a,b,c,d = abcd[i][0],abcd[i][1],abcd[i][2],abcd[i][3]

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

            result += d

    ans = max(ans, result)



print(ans)

import itertools



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

abcd = [list(map(int,input().split())) for i in range(q)]



ans = 0

for A in itertools.combinations_with_replacement(list(range(1,m+1)),n):

    tmp = 0

    for i in range(q):

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

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

            tmp += d

    ans = max(tmp, ans)



print(ans)

import itertools

n, m, q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for i in range(q)]
ans = 0
for iter in itertools.combinations_with_replacement(list(range(1, m + 1)), n):
    result = 0
    for i in range(q):
        a, b, c, d = abcd[i][0], abcd[i][1], abcd[i][2], abcd[i][3]
        if iter[b - 1] - iter[a - 1] == c:
            result += d
    ans = max(ans, result)
print(ans)

import itertools

n, m, q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for i in range(q)]
ans = 0
for A in itertools.combinations_with_replacement(list(range(1, m + 1)), n):
    tmp = 0
    for i in range(q):
        a, b, c, d = abcd[i]
        if A[b - 1] - A[a - 1] == c:
            tmp += d
    ans = max(tmp, ans)
print(ans)

-for iter in itertools.combinations_with_replacement(list(range(1, m + 1)), n): -    result = 0 +for A in itertools.combinations_with_replacement(list(range(1, m + 1)), n): +    tmp = 0 -        a, b, c, d = abcd[i][0], abcd[i][1], abcd[i][2], abcd[i][3] -        if iter[b - 1] - iter[a - 1] == c: -            result += d -    ans = max(ans, result) +        a, b, c, d = abcd[i] +        if A[b - 1] - A[a - 1] == c: +            tmp += d +    ans = max(tmp, ans)