import itertools

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

a = [0]*q

b = [0]*q

c = [0]*q

d = [0]*q

for i in range(q):

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

ans = 0

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

    tmp = 0

    for i in range(q):

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

            tmp += d[i]

    ans = max(ans,tmp)

print(ans)

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

a = [0]*q

b = [0]*q

c = [0]*q

d = [0]*q

for i in range(q):

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

    a[i] -= 1

    b[i] -= 1

A = [0]*n

ans = 0

def dfs(h):

    global ans

    if h == n-1:

        cnt = 0

        for i in range(q):

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

                cnt += d[i]

        ans = max(ans,cnt)

    else:

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

            A[h+1] = i

            dfs(h+1)

    return



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

    A[0] = i

    dfs(0)

print(ans)

import itertools

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

n, m, q = list(map(int, input().split()))
a = [0] * q
b = [0] * q
c = [0] * q
d = [0] * q
for i in range(q):
    a[i], b[i], c[i], d[i] = list(map(int, input().split()))
    a[i] -= 1
    b[i] -= 1
A = [0] * n
ans = 0


def dfs(h):
    global ans
    if h == n - 1:
        cnt = 0
        for i in range(q):
            if A[b[i]] - A[a[i]] == c[i]:
                cnt += d[i]
        ans = max(ans, cnt)
    else:
        for i in range(A[h], m + 1):
            A[h + 1] = i
            dfs(h + 1)
    return


for i in range(1, m + 1):
    A[0] = i
    dfs(0)
print(ans)

-import itertools - +    a[i] -= 1 +    b[i] -= 1 +A = [0] * n -for A in itertools.combinations_with_replacement(list(range(1, m + 1)), n): -    tmp = 0 -    for i in range(q): -        if A[b[i] - 1] - A[a[i] - 1] == c[i]: -            tmp += d[i] -    ans = max(ans, tmp) + + +def dfs(h): +    global ans +    if h == n - 1: +        cnt = 0 +        for i in range(q): +            if A[b[i]] - A[a[i]] == c[i]: +                cnt += d[i] +        ans = max(ans, cnt) +    else: +        for i in range(A[h], m + 1): +            A[h + 1] = i +            dfs(h + 1) +    return + + +for i in range(1, m + 1): +    A[0] = i +    dfs(0)