import sys

sys.setrecursionlimit(10**9)

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

abcd = [0 for _ in range(Q)]

for i in range(Q):

    abcd[i] = list(map(int, input().split()))



ans = 0

def dfs(A):

    global ans

    if len(A) == N:

        score = 0

        for i in range(Q):

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

                score += abcd[i][3]

        ans = max(ans, score)

    else:

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

            dfs(A + [i])



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

    dfs([j])

    

print(ans)

import sys

sys.setrecursionlimit(10**9)

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

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

ans = 0

def dfs(A):

    global ans

    if len(A) == N:

        score = 0

        for a,b,c,d in abcd:

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

                score += d

        ans = max(ans, score)

    else:

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

            dfs(A + [i])



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

    dfs([j])

    

print(ans)

import sys

sys.setrecursionlimit(10**9)
N, M, Q = list(map(int, input().split()))
abcd = [0 for _ in range(Q)]
for i in range(Q):
    abcd[i] = list(map(int, input().split()))
ans = 0


def dfs(A):
    global ans
    if len(A) == N:
        score = 0
        for i in range(Q):
            if A[abcd[i][1] - 1] - A[abcd[i][0] - 1] == abcd[i][2]:
                score += abcd[i][3]
        ans = max(ans, score)
    else:
        for i in range(A[-1], M + 1):
            dfs(A + [i])


for j in range(1, M + 1):
    dfs([j])
print(ans)

import sys

sys.setrecursionlimit(10**9)
N, M, Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for _ in range(Q)]
ans = 0


def dfs(A):
    global ans
    if len(A) == N:
        score = 0
        for a, b, c, d in abcd:
            if A[b - 1] - A[a - 1] == c:
                score += d
        ans = max(ans, score)
    else:
        for i in range(A[-1], M + 1):
            dfs(A + [i])


for j in range(1, M + 1):
    dfs([j])
print(ans)

-abcd = [0 for _ in range(Q)] -for i in range(Q): -    abcd[i] = list(map(int, input().split())) +abcd = [list(map(int, input().split())) for _ in range(Q)] -        for i in range(Q): -            if A[abcd[i][1] - 1] - A[abcd[i][0] - 1] == abcd[i][2]: -                score += abcd[i][3] +        for a, b, c, d in abcd: +            if A[b - 1] - A[a - 1] == c: +                score += d