import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)



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

pairs = [0] * q

for i in range(q):

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



from collections import deque



def dfs(min_val, length):

    if length == 0:

        return list([deque()])

    if min_val == m:

        return list([deque([m]) * length])

    ans = list()

    for i in range(min_val, m+1):

        remain = dfs(i, length-1)

        for j in remain:

            j.appendleft(i)

        ans.extend(remain)

    return ans



ans = 0

for i in dfs(1, n):

    tmp = 0

    for a, b, c, d in pairs:

        if (i[b-1] - i[a-1]) == c:

            tmp += d

    ans = max(ans, tmp)



print(ans)

import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)



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

abcd = [0] * q

for i in range(q):

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



def dfs(seq):

    if len(seq) == n:

        yield tuple(seq)

        return

    if len(seq) == 0:

        start = 1

    else:

        start = seq[-1]

    for i in range(start, m+1):

        new_seq = seq.copy()

        new_seq.append(i)

        yield from dfs(new_seq)



ans = 0

for s in dfs([]):

    tmp_ans = 0

    for a, b, c, d in abcd:

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

            tmp_ans += d

    ans = max(ans, tmp_ans)

print(ans)

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, m, q = list(map(int, input().split()))
pairs = [0] * q
for i in range(q):
    pairs[i] = list(map(int, input().split()))
from collections import deque


def dfs(min_val, length):
    if length == 0:
        return list([deque()])
    if min_val == m:
        return list([deque([m]) * length])
    ans = list()
    for i in range(min_val, m + 1):
        remain = dfs(i, length - 1)
        for j in remain:
            j.appendleft(i)
        ans.extend(remain)
    return ans


ans = 0
for i in dfs(1, n):
    tmp = 0
    for a, b, c, d in pairs:
        if (i[b - 1] - i[a - 1]) == c:
            tmp += d
    ans = max(ans, tmp)
print(ans)

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, m, q = list(map(int, input().split()))
abcd = [0] * q
for i in range(q):
    abcd[i] = list(map(int, input().split()))


def dfs(seq):
    if len(seq) == n:
        yield tuple(seq)
        return
    if len(seq) == 0:
        start = 1
    else:
        start = seq[-1]
    for i in range(start, m + 1):
        new_seq = seq.copy()
        new_seq.append(i)
        yield from dfs(new_seq)


ans = 0
for s in dfs([]):
    tmp_ans = 0
    for a, b, c, d in abcd:
        if s[b - 1] - s[a - 1] == c:
            tmp_ans += d
    ans = max(ans, tmp_ans)
print(ans)

-pairs = [0] * q +abcd = [0] * q -    pairs[i] = list(map(int, input().split())) -from collections import deque +    abcd[i] = list(map(int, input().split())) -def dfs(min_val, length): -    if length == 0: -        return list([deque()]) -    if min_val == m: -        return list([deque([m]) * length]) -    ans = list() -    for i in range(min_val, m + 1): -        remain = dfs(i, length - 1) -        for j in remain: -            j.appendleft(i) -        ans.extend(remain) -    return ans +def dfs(seq): +    if len(seq) == n: +        yield tuple(seq) +        return +    if len(seq) == 0: +        start = 1 +    else: +        start = seq[-1] +    for i in range(start, m + 1): +        new_seq = seq.copy() +        new_seq.append(i) +        yield from dfs(new_seq) -for i in dfs(1, n): -    tmp = 0 -    for a, b, c, d in pairs: -        if (i[b - 1] - i[a - 1]) == c: -            tmp += d -    ans = max(ans, tmp) +for s in dfs([]): +    tmp_ans = 0 +    for a, b, c, d in abcd: +        if s[b - 1] - s[a - 1] == c: +            tmp_ans += d +    ans = max(ans, tmp_ans)