import itertools



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

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

B = []

# print(len(list(A)))

for a in A:

    for j in range(N - 1):

        flag = True

        if a[j] > a[j + 1]:

            flag = False

            break

    if flag:

        B.append(a)



a = []

b = []

c = []

d = []



for i in range(Q):

    o, p, q, r = list(map(int, input().split()))

    o -= 1

    p -= 1

    a.append(o)

    b.append(p)

    c.append(q)

    d.append(r)



ans = 0

for li in B:

    tmp = 0

    for i in range(Q):

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

            tmp += d[i]

    ans = max(ans, tmp)

print(ans)

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



a = []

b = []

c = []

d = []



ans = 0





def dfs(A):

    global ans

    if len(A) == N:

        now = 0

        for i in range(Q):

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

                now += d[i]

        ans = max(now, ans)

        return



    if not A:  # Aが空リストのとき

        tmp = 1

    else:

        tmp = A[-1]



    while tmp <= M:

        dfs(A + [tmp])

        tmp += 1





for i in range(Q):

    o, p, q, r = list(map(int, input().split()))

    o -= 1

    p -= 1

    a.append(o)

    b.append(p)

    c.append(q)

    d.append(r)

dfs([])

print(ans)

import itertools

N, M, Q = list(map(int, input().split()))
A = list(itertools.combinations_with_replacement(list(range(1, M + 1)), N))
B = []
# print(len(list(A)))
for a in A:
    for j in range(N - 1):
        flag = True
        if a[j] > a[j + 1]:
            flag = False
            break
    if flag:
        B.append(a)
a = []
b = []
c = []
d = []
for i in range(Q):
    o, p, q, r = list(map(int, input().split()))
    o -= 1
    p -= 1
    a.append(o)
    b.append(p)
    c.append(q)
    d.append(r)
ans = 0
for li in B:
    tmp = 0
    for i in range(Q):
        if li[b[i]] - li[a[i]] == c[i]:
            tmp += d[i]
    ans = max(ans, tmp)
print(ans)

N, M, Q = list(map(int, input().split()))
a = []
b = []
c = []
d = []
ans = 0


def dfs(A):
    global ans
    if len(A) == N:
        now = 0
        for i in range(Q):
            if A[b[i]] - A[a[i]] == c[i]:
                now += d[i]
        ans = max(now, ans)
        return
    if not A:  # Aが空リストのとき
        tmp = 1
    else:
        tmp = A[-1]
    while tmp <= M:
        dfs(A + [tmp])
        tmp += 1


for i in range(Q):
    o, p, q, r = list(map(int, input().split()))
    o -= 1
    p -= 1
    a.append(o)
    b.append(p)
    c.append(q)
    d.append(r)
dfs([])
print(ans)

-import itertools - -A = list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)) -B = [] -# print(len(list(A))) -for a in A: -    for j in range(N - 1): -        flag = True -        if a[j] > a[j + 1]: -            flag = False -            break -    if flag: -        B.append(a) +ans = 0 + + +def dfs(A): +    global ans +    if len(A) == N: +        now = 0 +        for i in range(Q): +            if A[b[i]] - A[a[i]] == c[i]: +                now += d[i] +        ans = max(now, ans) +        return +    if not A:  # Aが空リストのとき +        tmp = 1 +    else: +        tmp = A[-1] +    while tmp <= M: +        dfs(A + [tmp]) +        tmp += 1 + + -ans = 0 -for li in B: -    tmp = 0 -    for i in range(Q): -        if li[b[i]] - li[a[i]] == c[i]: -            tmp += d[i] -    ans = max(ans, tmp) +dfs([])