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 seq

        return

    if len(seq) == 0:

        _max = 1

    else:

        _max = seq[-1]

    for i in range(_max, 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()))

abcd = [0] * q

for i in range(q):

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



def dfs(seq):

    if len(seq) == n:

        yield seq

        return

    else:

        if not seq:

            last = 1

        else:

            last = seq[-1]

    for i in range(last, 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()))
abcd = [0] * q
for i in range(q):
    abcd[i] = list(map(int, input().split()))


def dfs(seq):
    if len(seq) == n:
        yield seq
        return
    if len(seq) == 0:
        _max = 1
    else:
        _max = seq[-1]
    for i in range(_max, 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()))
abcd = [0] * q
for i in range(q):
    abcd[i] = list(map(int, input().split()))


def dfs(seq):
    if len(seq) == n:
        yield seq
        return
    else:
        if not seq:
            last = 1
        else:
            last = seq[-1]
    for i in range(last, 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)

-    if len(seq) == 0: -        _max = 1 -        _max = seq[-1] -    for i in range(_max, m + 1): +        if not seq: +            last = 1 +        else: +            last = seq[-1] +    for i in range(last, m + 1): -        if (s[b - 1] - s[a - 1]) == c: +        if s[b - 1] - s[a - 1] == c: