import itertools



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

kumi = []

for i in range(q):

    kumii = list(map(int, input().split()))

    kumi.append(kumii)



ans = 0

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

    temp = 0

    for i in range(q):

        ai, bi, ci, di = kumi[i]

        ai, bi = ai-1, bi-1

        if a[bi] - a[ai] == ci:

            temp += di

    ans = max(ans, temp)



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

kumi = []

for i in range(q):

    kumii = list(map(int, input().split()))

    kumi.append(kumii)



ans = 0

a = [0 for i in range(n)]

def dfs(p):

    global ans

    if p == 0:

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

            a[p] = i

            dfs(p+1)

    else:

        for i in range(a[p-1], m+1):

            a[p] = i

            if p != n-1:

                dfs(p+1)

            else:

                temp = 0

                for j in range(q):

                    ai, bi, ci, di = kumi[j]

                    ai, bi = ai-1, bi-1

                    if a[bi] - a[ai] == ci:

                        temp += di

                ans = max(ans, temp)



dfs(0)

print(ans)
import itertools

n, m, q = list(map(int, input().split()))
kumi = []
for i in range(q):
    kumii = list(map(int, input().split()))
    kumi.append(kumii)
ans = 0
for a in itertools.combinations_with_replacement(list(range(1, m + 1)), n):
    temp = 0
    for i in range(q):
        ai, bi, ci, di = kumi[i]
        ai, bi = ai - 1, bi - 1
        if a[bi] - a[ai] == ci:
            temp += di
    ans = max(ans, temp)
print(ans)

n, m, q = list(map(int, input().split()))
kumi = []
for i in range(q):
    kumii = list(map(int, input().split()))
    kumi.append(kumii)
ans = 0
a = [0 for i in range(n)]


def dfs(p):
    global ans
    if p == 0:
        for i in range(1, m + 1):
            a[p] = i
            dfs(p + 1)
    else:
        for i in range(a[p - 1], m + 1):
            a[p] = i
            if p != n - 1:
                dfs(p + 1)
            else:
                temp = 0
                for j in range(q):
                    ai, bi, ci, di = kumi[j]
                    ai, bi = ai - 1, bi - 1
                    if a[bi] - a[ai] == ci:
                        temp += di
                ans = max(ans, temp)


dfs(0)
print(ans)

-import itertools - -for a in itertools.combinations_with_replacement(list(range(1, m + 1)), n): -    temp = 0 -    for i in range(q): -        ai, bi, ci, di = kumi[i] -        ai, bi = ai - 1, bi - 1 -        if a[bi] - a[ai] == ci: -            temp += di -    ans = max(ans, temp) +a = [0 for i in range(n)] + + +def dfs(p): +    global ans +    if p == 0: +        for i in range(1, m + 1): +            a[p] = i +            dfs(p + 1) +    else: +        for i in range(a[p - 1], m + 1): +            a[p] = i +            if p != n - 1: +                dfs(p + 1) +            else: +                temp = 0 +                for j in range(q): +                    ai, bi, ci, di = kumi[j] +                    ai, bi = ai - 1, bi - 1 +                    if a[bi] - a[ai] == ci: +                        temp += di +                ans = max(ans, temp) + + +dfs(0)