import sys

sys.setrecursionlimit(10 ** 6)

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

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

for _ in range(Q):

    ta, tb, tc, td = list(map(int, input().split()))

    ta -= 1

    tb -= 1

    a.append(ta)

    b.append(tb)

    c.append(tc)

    d.append(td)



x = []

def dfs(s):

    if len(s) - s.count("0") == N:

        x.append(s)

        return



    if s[-1] == "0":

        start = 10

    else:

        start = s[-1]

    for i in range(int(start), M+1):

        dfs(s + str(i))

        # x.append(res)



ans = 0

for i in range(1, M+1):

    x = []

    dfs(str(i))

    # print(x)

    for y in x:

        A = []

        for z in y:

            if z == "0":

                A[-1] += z

            else:

                A.append(z)

        A = [int(z) for z in A]

        # print(A)

        score = 0

        for ta, tb, tc, td in zip(a, b, c, d):

            if A[int(tb)] - A[int(ta)] == tc:

                score += td

        ans = max(ans, score)

print(ans)

from copy import deepcopy

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

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

for i in range(Q):

    tmp = list(map(int, input().split()))

    a.append(tmp[0])

    b.append(tmp[1])

    c.append(tmp[2])

    d.append(tmp[3])



ans = 0

def dfs(A):

    global ans

    if len(A) == N + 1:

        now = 0

        for i in range(Q):

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

                now += d[i]

        ans = max(ans, now)

        return



    A.append(A[-1])

    while A[-1] <= M:

        dfs(deepcopy(A))

        A[-1] += 1



dfs([1])

print(ans)
import sys

sys.setrecursionlimit(10**6)
N, M, Q = list(map(int, input().split()))
a, b, c, d = [], [], [], []
for _ in range(Q):
    ta, tb, tc, td = list(map(int, input().split()))
    ta -= 1
    tb -= 1
    a.append(ta)
    b.append(tb)
    c.append(tc)
    d.append(td)
x = []


def dfs(s):
    if len(s) - s.count("0") == N:
        x.append(s)
        return
    if s[-1] == "0":
        start = 10
    else:
        start = s[-1]
    for i in range(int(start), M + 1):
        dfs(s + str(i))
        # x.append(res)


ans = 0
for i in range(1, M + 1):
    x = []
    dfs(str(i))
    # print(x)
    for y in x:
        A = []
        for z in y:
            if z == "0":
                A[-1] += z
            else:
                A.append(z)
        A = [int(z) for z in A]
        # print(A)
        score = 0
        for ta, tb, tc, td in zip(a, b, c, d):
            if A[int(tb)] - A[int(ta)] == tc:
                score += td
        ans = max(ans, score)
print(ans)

from copy import deepcopy

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


def dfs(A):
    global ans
    if len(A) == N + 1:
        now = 0
        for i in range(Q):
            if A[b[i]] - A[a[i]] == c[i]:
                now += d[i]
        ans = max(ans, now)
        return
    A.append(A[-1])
    while A[-1] <= M:
        dfs(deepcopy(A))
        A[-1] += 1


dfs([1])
print(ans)

-import sys +from copy import deepcopy -sys.setrecursionlimit(10**6) -for _ in range(Q): -    ta, tb, tc, td = list(map(int, input().split())) -    ta -= 1 -    tb -= 1 -    a.append(ta) -    b.append(tb) -    c.append(tc) -    d.append(td) -x = [] +for i in range(Q): +    tmp = list(map(int, input().split())) +    a.append(tmp[0]) +    b.append(tmp[1]) +    c.append(tmp[2]) +    d.append(tmp[3]) +ans = 0 -def dfs(s): -    if len(s) - s.count("0") == N: -        x.append(s) +def dfs(A): +    global ans +    if len(A) == N + 1: +        now = 0 +        for i in range(Q): +            if A[b[i]] - A[a[i]] == c[i]: +                now += d[i] +        ans = max(ans, now) -    if s[-1] == "0": -        start = 10 -    else: -        start = s[-1] -    for i in range(int(start), M + 1): -        dfs(s + str(i)) -        # x.append(res) +    A.append(A[-1]) +    while A[-1] <= M: +        dfs(deepcopy(A)) +        A[-1] += 1 -ans = 0 -for i in range(1, M + 1): -    x = [] -    dfs(str(i)) -    # print(x) -    for y in x: -        A = [] -        for z in y: -            if z == "0": -                A[-1] += z -            else: -                A.append(z) -        A = [int(z) for z in A] -        # print(A) -        score = 0 -        for ta, tb, tc, td in zip(a, b, c, d): -            if A[int(tb)] - A[int(ta)] == tc: -                score += td -        ans = max(ans, score) +dfs([1])