import sys

from copy import deepcopy

sys.setrecursionlimit(10**7)





def dfs(now):

    global ans

    if len(now) == N:

        tmp_ans = 0

        for a, b, c, d in query:

            a -= 1

            b -= 1

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

                tmp_ans += d

        ans = max(tmp_ans, ans)

    else:

        for i in range(now[-1], M+1):

            tmp = deepcopy(now)

            tmp.append(i)

            dfs(tmp)





N, M, Q = list(map(int, input().split()))  # N個の数列, 上限M, Q個のクエリ

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



A = [1]

ans = 0

dfs(A)

print(ans)

# 全探索で行う

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

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

A = [1]



def dfs(now):

    if len(now) == N:

        global ans

        tmp_ans = 0

        for a, b, c, d in query:

            a -= 1

            b -= 1

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

                tmp_ans += d

        ans = max(ans, tmp_ans)

    else:

        for i in range(now[-1], M+1):

            tmp = now.copy()

            tmp.append(i)

            dfs(tmp)





ans = 0

dfs(A)

print(ans)

import sys
from copy import deepcopy

sys.setrecursionlimit(10**7)


def dfs(now):
    global ans
    if len(now) == N:
        tmp_ans = 0
        for a, b, c, d in query:
            a -= 1
            b -= 1
            if now[b] - now[a] == c:
                tmp_ans += d
        ans = max(tmp_ans, ans)
    else:
        for i in range(now[-1], M + 1):
            tmp = deepcopy(now)
            tmp.append(i)
            dfs(tmp)


N, M, Q = list(map(int, input().split()))  # N個の数列, 上限M, Q個のクエリ
query = [list(map(int, input().split())) for _ in range(Q)]
A = [1]
ans = 0
dfs(A)
print(ans)

# 全探索で行う
N, M, Q = list(map(int, input().split()))
query = [list(map(int, input().split())) for _ in range(Q)]
A = [1]


def dfs(now):
    if len(now) == N:
        global ans
        tmp_ans = 0
        for a, b, c, d in query:
            a -= 1
            b -= 1
            if now[b] - now[a] == c:
                tmp_ans += d
        ans = max(ans, tmp_ans)
    else:
        for i in range(now[-1], M + 1):
            tmp = now.copy()
            tmp.append(i)
            dfs(tmp)


ans = 0
dfs(A)
print(ans)

-import sys -from copy import deepcopy - -sys.setrecursionlimit(10**7) +# 全探索で行う +N, M, Q = list(map(int, input().split())) +query = [list(map(int, input().split())) for _ in range(Q)] +A = [1] -    global ans +        global ans -        ans = max(tmp_ans, ans) +        ans = max(ans, tmp_ans) -            tmp = deepcopy(now) +            tmp = now.copy() -N, M, Q = list(map(int, input().split()))  # N個の数列, 上限M, Q個のクエリ -query = [list(map(int, input().split())) for _ in range(Q)] -A = [1]