def resolve():

    import itertools

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

    cond = [list(map(int, input().split())) for _ in range(q)]

    ans = 0

    for i in list(itertools.combinations_with_replacement(list(range(1, m+1)), n)):

        point = 0

        for j in range(q):

            if i[cond[j][1]-1] - i[cond[j][0]-1] == cond[j][2]:

                point += cond[j][3]

        ans = max(ans, point)

    print(ans)

resolve()
def resolve():

    import itertools

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

    cond = [list(map(int, input().split())) for _ in range(q)]

    ans = 0

    for i in itertools.combinations_with_replacement(list(range(1, m+1)), n):

        point = 0

        for j in range(q):

            if i[cond[j][1]-1] - i[cond[j][0]-1] == cond[j][2]:

                point += cond[j][3]

        ans = max(ans, point)

    print(ans)

resolve()
def resolve():
    import itertools

    n, m, q = list(map(int, input().split()))
    cond = [list(map(int, input().split())) for _ in range(q)]
    ans = 0
    for i in list(itertools.combinations_with_replacement(list(range(1, m + 1)), n)):
        point = 0
        for j in range(q):
            if i[cond[j][1] - 1] - i[cond[j][0] - 1] == cond[j][2]:
                point += cond[j][3]
        ans = max(ans, point)
    print(ans)


resolve()

def resolve():
    import itertools

    n, m, q = list(map(int, input().split()))
    cond = [list(map(int, input().split())) for _ in range(q)]
    ans = 0
    for i in itertools.combinations_with_replacement(list(range(1, m + 1)), n):
        point = 0
        for j in range(q):
            if i[cond[j][1] - 1] - i[cond[j][0] - 1] == cond[j][2]:
                point += cond[j][3]
        ans = max(ans, point)
    print(ans)


resolve()

-    for i in list(itertools.combinations_with_replacement(list(range(1, m + 1)), n)): +    for i in itertools.combinations_with_replacement(list(range(1, m + 1)), n):