import itertools



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

AA = itertools.combinations_with_replacement(list(range(1,M+1)), N)

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



ans = -10**18

for A in AA:

    score = 0

    for a,b,c,d in abcd:

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

            score += d

    ans = max(ans, score)

print(ans)
import sys

import itertools

import numpy as np



read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



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

A = np.array(list(itertools.combinations_with_replacement(list(range(1,M+1)), N)))



score = np.zeros(len(A), int)

m = list(map(int, read().split()))

for a,b,c,d in zip(m,m,m,m):

    score += (A[:,b-1] - A[:,a-1] == c) * d

print((score.max()))
import itertools

N, M, Q = list(map(int, input().split()))
AA = itertools.combinations_with_replacement(list(range(1, M + 1)), N)
abcd = [list(map(int, input().split())) for _ in range(Q)]
ans = -(10**18)
for A in AA:
    score = 0
    for a, b, c, d in abcd:
        if A[b - 1] - A[a - 1] == c:
            score += d
    ans = max(ans, score)
print(ans)

import sys
import itertools
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, M, Q = list(map(int, readline().split()))
A = np.array(list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)))
score = np.zeros(len(A), int)
m = list(map(int, read().split()))
for a, b, c, d in zip(m, m, m, m):
    score += (A[:, b - 1] - A[:, a - 1] == c) * d
print((score.max()))

+import sys +import numpy as np -N, M, Q = list(map(int, input().split())) -AA = itertools.combinations_with_replacement(list(range(1, M + 1)), N) -abcd = [list(map(int, input().split())) for _ in range(Q)] -ans = -(10**18) -for A in AA: -    score = 0 -    for a, b, c, d in abcd: -        if A[b - 1] - A[a - 1] == c: -            score += d -    ans = max(ans, score) -print(ans) +read = sys.stdin.buffer.read +readline = sys.stdin.buffer.readline +readlines = sys.stdin.buffer.readlines +N, M, Q = list(map(int, readline().split())) +A = np.array(list(itertools.combinations_with_replacement(list(range(1, M + 1)), N))) +score = np.zeros(len(A), int) +m = list(map(int, read().split())) +for a, b, c, d in zip(m, m, m, m): +    score += (A[:, b - 1] - A[:, a - 1] == c) * d +print((score.max()))