import itertools

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

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



l = list(itertools.combinations_with_replacement([i+1 for i in range(m)], n))



MAX=0

for i in l:

    ans=0

    for g in range(q):

        a,b,c,d = Q[g]

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

            ans+=d

    MAX=max(ans,MAX)

print(MAX)

def dfs(lst):

    global ans

    if len(lst) > n:

        return

    lowest = 1

    if lst != []:

        lowest = lst[-1]

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

        lst.append(i)

        dfs(lst)

        lst.pop()

    if len(lst) == n:

        cnt = 0

        for i in range(q):

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

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

                cnt += d

        ans = max(cnt, ans)



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

ab = [tuple(map(int, input().split())) for _ in range(q)]

ans = 0

dfs([])



print(ans)
import itertools

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

def dfs(lst):
    global ans
    if len(lst) > n:
        return
    lowest = 1
    if lst != []:
        lowest = lst[-1]
    for i in range(lowest, m + 1):
        lst.append(i)
        dfs(lst)
        lst.pop()
    if len(lst) == n:
        cnt = 0
        for i in range(q):
            a, b, c, d = ab[i]
            if lst[b - 1] - lst[a - 1] == c:
                cnt += d
        ans = max(cnt, ans)


n, m, q = list(map(int, input().split()))
ab = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0
dfs([])
print(ans)

-import itertools +def dfs(lst): +    global ans +    if len(lst) > n: +        return +    lowest = 1 +    if lst != []: +        lowest = lst[-1] +    for i in range(lowest, m + 1): +        lst.append(i) +        dfs(lst) +        lst.pop() +    if len(lst) == n: +        cnt = 0 +        for i in range(q): +            a, b, c, d = ab[i] +            if lst[b - 1] - lst[a - 1] == c: +                cnt += d +        ans = max(cnt, ans) + -Q = [list(map(int, input().split())) for i in range(q)] -l = list(itertools.combinations_with_replacement([i + 1 for i in range(m)], n)) -MAX = 0 -for i in l: -    ans = 0 -    for g in range(q): -        a, b, c, d = Q[g] -        if i[b - 1] - i[a - 1] == c: -            ans += d -    MAX = max(ans, MAX) -print(MAX) +ab = [tuple(map(int, input().split())) for _ in range(q)] +ans = 0 +dfs([]) +print(ans)