import itertools

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

num_ls = [[0,0,0,0] for _ in range(q)]

for i in range(q):

    num_ls[i] = list(map(int,input().split()))



a_base = [i for i in range(1,m+1)]

a_ls = []

for A in itertools.combinations_with_replacement(a_base, n):

    istarget = True

    for i in range(n-1):

        if A[i] > A[i+1]:

            istarget = False

    if istarget:

        a_ls.append(list(A))

ans = 0

for A in a_ls:

    value_of_A = 0

    for a,b,c,d in num_ls:

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

            value_of_A += d

    ans = max(ans, value_of_A)

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

req_ls = []

l_ind_ls = [0] * Q

r_ind_ls = [0] * Q

diff_ls = [0] * Q

point_ls = [0] * Q

for i in range(Q):

    l_ind_ls[i], r_ind_ls[i], diff_ls[i], point_ls[i] = list(map(int, input().split()))

    l_ind_ls[i] -= 1

    r_ind_ls[i] -= 1



def score(ls):

    tmp = 0

    for l_ind, r_ind, diff, point in zip(l_ind_ls,r_ind_ls,diff_ls,point_ls):

        if ls[r_ind] - ls[l_ind] == diff:

            tmp += point

    return tmp



def dfs(ls):

    if len(ls) == N:

        return score(ls)

    res = 0

    last = ls[-1] if len(ls) > 0 else 1

    for nex in range(last, M+1):

        ls.append(nex)

        res = max(res, dfs(ls))

        ls.pop()

    return res

print((dfs([])))





import itertools

n, m, q = list(map(int, input().split()))
num_ls = [[0, 0, 0, 0] for _ in range(q)]
for i in range(q):
    num_ls[i] = list(map(int, input().split()))
a_base = [i for i in range(1, m + 1)]
a_ls = []
for A in itertools.combinations_with_replacement(a_base, n):
    istarget = True
    for i in range(n - 1):
        if A[i] > A[i + 1]:
            istarget = False
    if istarget:
        a_ls.append(list(A))
ans = 0
for A in a_ls:
    value_of_A = 0
    for a, b, c, d in num_ls:
        if A[b - 1] - A[a - 1] == c:
            value_of_A += d
    ans = max(ans, value_of_A)
print(ans)

N, M, Q = list(map(int, input().split()))
req_ls = []
l_ind_ls = [0] * Q
r_ind_ls = [0] * Q
diff_ls = [0] * Q
point_ls = [0] * Q
for i in range(Q):
    l_ind_ls[i], r_ind_ls[i], diff_ls[i], point_ls[i] = list(map(int, input().split()))
    l_ind_ls[i] -= 1
    r_ind_ls[i] -= 1


def score(ls):
    tmp = 0
    for l_ind, r_ind, diff, point in zip(l_ind_ls, r_ind_ls, diff_ls, point_ls):
        if ls[r_ind] - ls[l_ind] == diff:
            tmp += point
    return tmp


def dfs(ls):
    if len(ls) == N:
        return score(ls)
    res = 0
    last = ls[-1] if len(ls) > 0 else 1
    for nex in range(last, M + 1):
        ls.append(nex)
        res = max(res, dfs(ls))
        ls.pop()
    return res


print((dfs([])))

-import itertools +N, M, Q = list(map(int, input().split())) +req_ls = [] +l_ind_ls = [0] * Q +r_ind_ls = [0] * Q +diff_ls = [0] * Q +point_ls = [0] * Q +for i in range(Q): +    l_ind_ls[i], r_ind_ls[i], diff_ls[i], point_ls[i] = list(map(int, input().split())) +    l_ind_ls[i] -= 1 +    r_ind_ls[i] -= 1 -n, m, q = list(map(int, input().split())) -num_ls = [[0, 0, 0, 0] for _ in range(q)] -for i in range(q): -    num_ls[i] = list(map(int, input().split())) -a_base = [i for i in range(1, m + 1)] -a_ls = [] -for A in itertools.combinations_with_replacement(a_base, n): -    istarget = True -    for i in range(n - 1): -        if A[i] > A[i + 1]: -            istarget = False -    if istarget: -        a_ls.append(list(A)) -ans = 0 -for A in a_ls: -    value_of_A = 0 -    for a, b, c, d in num_ls: -        if A[b - 1] - A[a - 1] == c: -            value_of_A += d -    ans = max(ans, value_of_A) -print(ans) + +def score(ls): +    tmp = 0 +    for l_ind, r_ind, diff, point in zip(l_ind_ls, r_ind_ls, diff_ls, point_ls): +        if ls[r_ind] - ls[l_ind] == diff: +            tmp += point +    return tmp + + +def dfs(ls): +    if len(ls) == N: +        return score(ls) +    res = 0 +    last = ls[-1] if len(ls) > 0 else 1 +    for nex in range(last, M + 1): +        ls.append(nex) +        res = max(res, dfs(ls)) +        ls.pop() +    return res + + +print((dfs([])))