import sys

input = sys.stdin.buffer.readline



from heapq import heappush, heappop

from bisect import bisect_left, bisect_right

from collections import deque, defaultdict

from itertools import combinations, permutations

from itertools import accumulate

from math import ceil, sqrt, pi



MOD = 10 ** 9 + 7

INF = 10 ** 18





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

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

#print(ABCD)



lst = []



def dfs(i, n, tmp):

    if n == N:

        lst.append(tmp)

        return 0

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

        dfs(j, n + 1, tmp + [j])



dfs(1, 0, [])

#print(lst)



ans = 0

for l in lst:

    tmp = 0

    for abcd in ABCD:

        a, b, c, d = abcd

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

            tmp += d

    ans = max(tmp, ans)

    

print(ans)
import sys

input = sys.stdin.buffer.readline



from itertools import combinations_with_replacement



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

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



lst = list(combinations_with_replacement(list(range(1, M + 1)), N))



ans = 0

for l in lst:

    tmp = 0

    for a, b, c, d in ABCD:

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

            tmp += d

    ans = max(tmp, ans)

    

print(ans)
import sys

input = sys.stdin.buffer.readline
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict
from itertools import combinations, permutations
from itertools import accumulate
from math import ceil, sqrt, pi

MOD = 10**9 + 7
INF = 10**18
N, M, Q = list(map(int, input().split()))
ABCD = [list(map(int, input().split())) for _ in range(Q)]
# print(ABCD)
lst = []


def dfs(i, n, tmp):
    if n == N:
        lst.append(tmp)
        return 0
    for j in range(i, M + 1):
        dfs(j, n + 1, tmp + [j])


dfs(1, 0, [])
# print(lst)
ans = 0
for l in lst:
    tmp = 0
    for abcd in ABCD:
        a, b, c, d = abcd
        if l[b - 1] - l[a - 1] == c:
            tmp += d
    ans = max(tmp, ans)
print(ans)

import sys

input = sys.stdin.buffer.readline
from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))
ABCD = [list(map(int, input().split())) for _ in range(Q)]
lst = list(combinations_with_replacement(list(range(1, M + 1)), N))
ans = 0
for l in lst:
    tmp = 0
    for a, b, c, d in ABCD:
        if l[b - 1] - l[a - 1] == c:
            tmp += d
    ans = max(tmp, ans)
print(ans)

-from heapq import heappush, heappop -from bisect import bisect_left, bisect_right -from collections import deque, defaultdict -from itertools import combinations, permutations -from itertools import accumulate -from math import ceil, sqrt, pi +from itertools import combinations_with_replacement -MOD = 10**9 + 7 -INF = 10**18 -# print(ABCD) -lst = [] - - -def dfs(i, n, tmp): -    if n == N: -        lst.append(tmp) -        return 0 -    for j in range(i, M + 1): -        dfs(j, n + 1, tmp + [j]) - - -dfs(1, 0, []) -# print(lst) +lst = list(combinations_with_replacement(list(range(1, M + 1)), N)) -    for abcd in ABCD: -        a, b, c, d = abcd +    for a, b, c, d in ABCD: