import copy

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 = [1]

ans = 0

def dfs(lis):

    if len(lis) == n+1:

        now = 0

        for i in range(q):

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

                now += d[i]

        global ans

        ans = max(now, ans)

        return 0

    lis.append(lis[-1])

    while lis[-1] <= m:

        newlis = copy.deepcopy(lis)

        dfs(newlis)

        lis[-1] += 1

dfs(A)

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):

    now = 0

    for i in range(q):

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

            now += d[i]

    ans = max(ans, now)

print(ans)
import copy

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 = [1]
ans = 0


def dfs(lis):
    if len(lis) == n + 1:
        now = 0
        for i in range(q):
            if lis[b[i]] - lis[a[i]] == c[i]:
                now += d[i]
        global ans
        ans = max(now, ans)
        return 0
    lis.append(lis[-1])
    while lis[-1] <= m:
        newlis = copy.deepcopy(lis)
        dfs(newlis)
        lis[-1] += 1


dfs(A)
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):
    now = 0
    for i in range(q):
        if A[b[i] - 1] - A[a[i] - 1] == c[i]:
            now += d[i]
    ans = max(ans, now)
print(ans)

-import copy +import itertools -A = [1] - - -def dfs(lis): -    if len(lis) == n + 1: -        now = 0 -        for i in range(q): -            if lis[b[i]] - lis[a[i]] == c[i]: -                now += d[i] -        global ans -        ans = max(now, ans) -        return 0 -    lis.append(lis[-1]) -    while lis[-1] <= m: -        newlis = copy.deepcopy(lis) -        dfs(newlis) -        lis[-1] += 1 - - -dfs(A) +for A in itertools.combinations_with_replacement(list(range(1, m + 1)), n): +    now = 0 +    for i in range(q): +        if A[b[i] - 1] - A[a[i] - 1] == c[i]: +            now += d[i] +    ans = max(ans, now)