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([])))





length, Max, q = list(map(int,input().split()))

query = [[0]*4 for _ in range(q)]

for i in range(q):

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

    query[i] = [a-1,b-1,c,d]

ans = 0

def dfs(A):

    if len(A) == length:

        global ans

        score = 0

        for i in range(q):

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

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

                score += d

        ans = max(ans, score)

        return

    last = A[-1]

    for i in range(last,Max+1):

        A.append(i)

        dfs(A)

        A.pop()

dfs([1])

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([])))

length, Max, q = list(map(int, input().split()))
query = [[0] * 4 for _ in range(q)]
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    query[i] = [a - 1, b - 1, c, d]
ans = 0


def dfs(A):
    if len(A) == length:
        global ans
        score = 0
        for i in range(q):
            a, b, c, d = query[i]
            if A[b] - A[a] == c:
                score += d
        ans = max(ans, score)
        return
    last = A[-1]
    for i in range(last, Max + 1):
        A.append(i)
        dfs(A)
        A.pop()


dfs([1])
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 +length, Max, q = list(map(int, input().split())) +query = [[0] * 4 for _ in range(q)] +for i in range(q): +    a, b, c, d = list(map(int, input().split())) +    query[i] = [a - 1, b - 1, c, d] +ans = 0 -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(A): +    if len(A) == length: +        global ans +        score = 0 +        for i in range(q): +            a, b, c, d = query[i] +            if A[b] - A[a] == c: +                score += d +        ans = max(ans, score) +        return +    last = A[-1] +    for i in range(last, Max + 1): +        A.append(i) +        dfs(A) +        A.pop() -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([]))) +dfs([1]) +print(ans)