N, M, Q = list(map(int, input().split()))

G = []

for i in range(Q):

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

    G.append([a, b, c, d])



ans = 0





def dfs(s):

    global ans

    if len(s) == N:

        now = 0

        for a, b, c, d in G:

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

                now += d

        ans = max(ans, now)

        return



    for i in range(s[-1], M):

        dfs(s + [i])





s = [0]

dfs(s.copy())



print(ans)

import numpy as np

import itertools

N, M, Q = list(map(int, input().split()))

G = []

for i in range(Q):

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

    G.append([a, b, c, d])



ans = 0



A = np.array(list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)))



n = len(A)

score = np.zeros(n, np.int32)

for a, b, c, d in G:

    cond = A[:, b - 1] - A[:, a - 1] == c

    score += d * cond



print((score.max()))

N, M, Q = list(map(int, input().split()))
G = []
for i in range(Q):
    a, b, c, d = list(map(int, input().split()))
    G.append([a, b, c, d])
ans = 0


def dfs(s):
    global ans
    if len(s) == N:
        now = 0
        for a, b, c, d in G:
            if s[b - 1] - s[a - 1] == c:
                now += d
        ans = max(ans, now)
        return
    for i in range(s[-1], M):
        dfs(s + [i])


s = [0]
dfs(s.copy())
print(ans)

import numpy as np
import itertools

N, M, Q = list(map(int, input().split()))
G = []
for i in range(Q):
    a, b, c, d = list(map(int, input().split()))
    G.append([a, b, c, d])
ans = 0
A = np.array(list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)))
n = len(A)
score = np.zeros(n, np.int32)
for a, b, c, d in G:
    cond = A[:, b - 1] - A[:, a - 1] == c
    score += d * cond
print((score.max()))

+import numpy as np +import itertools + - - -def dfs(s): -    global ans -    if len(s) == N: -        now = 0 -        for a, b, c, d in G: -            if s[b - 1] - s[a - 1] == c: -                now += d -        ans = max(ans, now) -        return -    for i in range(s[-1], M): -        dfs(s + [i]) - - -s = [0] -dfs(s.copy()) -print(ans) +A = np.array(list(itertools.combinations_with_replacement(list(range(1, M + 1)), N))) +n = len(A) +score = np.zeros(n, np.int32) +for a, b, c, d in G: +    cond = A[:, b - 1] - A[:, a - 1] == c +    score += d * cond +print((score.max()))