import itertools

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

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

ans = 0



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

    flag = 0

    for k in range(n - 1):

        if A[k + 1] < A[k]:

            flag = 1

    if flag == 0:

        point = 0

        for l in range(q):

            if A[lst[l][1] - 1] - A[lst[l][0] - 1] == lst[l][2]:

                point += lst[l][3]

        ans = max(ans, point)



print(ans)

import sys

sys.setrecursionlimit(1000000)

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

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

ans = 0



def dfs(A):

    global ans

    if len(A) == n + 1:

        point = 0

        for i in range(q):

            if A[lst[i][1]] - A[lst[i][0]] == lst[i][2]:

                point += lst[i][3]

                ans = max(ans, point)

        return

    A.append(A[-1])

    while A[-1] <= m:

        dfs(A[:])

        A[-1] += 1



dfs([1])



print(ans)

import itertools

n, m, q = list(map(int, input().split()))
lst = [list(map(int, input().split())) for i in range(q)]
ans = 0
for A in itertools.combinations_with_replacement(list(range(1, m + 1)), n):
    flag = 0
    for k in range(n - 1):
        if A[k + 1] < A[k]:
            flag = 1
    if flag == 0:
        point = 0
        for l in range(q):
            if A[lst[l][1] - 1] - A[lst[l][0] - 1] == lst[l][2]:
                point += lst[l][3]
        ans = max(ans, point)
print(ans)

import sys

sys.setrecursionlimit(1000000)
n, m, q = list(map(int, input().split()))
lst = [list(map(int, input().split())) for i in range(q)]
ans = 0


def dfs(A):
    global ans
    if len(A) == n + 1:
        point = 0
        for i in range(q):
            if A[lst[i][1]] - A[lst[i][0]] == lst[i][2]:
                point += lst[i][3]
                ans = max(ans, point)
        return
    A.append(A[-1])
    while A[-1] <= m:
        dfs(A[:])
        A[-1] += 1


dfs([1])
print(ans)

-import itertools +import sys +sys.setrecursionlimit(1000000) -for A in itertools.combinations_with_replacement(list(range(1, m + 1)), n): -    flag = 0 -    for k in range(n - 1): -        if A[k + 1] < A[k]: -            flag = 1 -    if flag == 0: + + +def dfs(A): +    global ans +    if len(A) == n + 1: -        for l in range(q): -            if A[lst[l][1] - 1] - A[lst[l][0] - 1] == lst[l][2]: -                point += lst[l][3] -        ans = max(ans, point) +        for i in range(q): +            if A[lst[i][1]] - A[lst[i][0]] == lst[i][2]: +                point += lst[i][3] +                ans = max(ans, point) +        return +    A.append(A[-1]) +    while A[-1] <= m: +        dfs(A[:]) +        A[-1] += 1 + + +dfs([1])