from itertools import permutations, accumulate, combinations, combinations_with_replacement

import sys

input = sys.stdin.readline





def main():

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

    # <方針>：Aを全列挙する

    ans = 0

    a = [0] * q

    b = [0] * q

    c = [0] * q

    d = [0] * q

    for i in range(q):

        a[i], b[i], c[i], d[i] = list(map(int, input().split()))

        a[i] -= 1

        b[i] -= 1

    for A in combinations_with_replacement(list(range(1, m + 1)), n):

        tmp = 0

        for i in range(q):

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

                tmp += d[i]

        ans = max(ans, tmp)

    print(ans)





if __name__ == '__main__':

    main()
import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 6)

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

l = [tuple(map(int, input().split())) for _ in range(q)]

ans = 0





def dfs(u, A):

    global n, m, q, l, ans

    if len(A) == n:  # lの長さがnになったらscoreの計算をする

        score = 0

        for i in range(q):

            a, b, c, d = l[i]

            a, b,  = a - 1, b - 1

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

                score += d

        ans = max(ans, score)

    else:  # 再帰的にリストに番号を追加していく

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

            dfs(i, A + [i])





def main():

    dfs(1, [1])

    print(ans)





if __name__ == '__main__':

    main()
from itertools import (
    permutations,
    accumulate,
    combinations,
    combinations_with_replacement,
)
import sys

input = sys.stdin.readline


def main():
    n, m, q = list(map(int, input().split()))
    # <方針>：Aを全列挙する
    ans = 0
    a = [0] * q
    b = [0] * q
    c = [0] * q
    d = [0] * q
    for i in range(q):
        a[i], b[i], c[i], d[i] = list(map(int, input().split()))
        a[i] -= 1
        b[i] -= 1
    for A in combinations_with_replacement(list(range(1, m + 1)), n):
        tmp = 0
        for i in range(q):
            if A[b[i]] - A[a[i]] == c[i]:
                tmp += d[i]
        ans = max(ans, tmp)
    print(ans)


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**6)
n, m, q = list(map(int, input().split()))
l = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0


def dfs(u, A):
    global n, m, q, l, ans
    if len(A) == n:  # lの長さがnになったらscoreの計算をする
        score = 0
        for i in range(q):
            a, b, c, d = l[i]
            a, b, = (
                a - 1,
                b - 1,
            )
            if A[b] - A[a] == c:
                score += d
        ans = max(ans, score)
    else:  # 再帰的にリストに番号を追加していく
        for i in range(u, m + 1):
            dfs(i, A + [i])


def main():
    dfs(1, [1])
    print(ans)


if __name__ == "__main__":
    main()

-from itertools import ( -    permutations, -    accumulate, -    combinations, -    combinations_with_replacement, -) +sys.setrecursionlimit(10**6) +n, m, q = list(map(int, input().split())) +l = [tuple(map(int, input().split())) for _ in range(q)] +ans = 0 + + +def dfs(u, A): +    global n, m, q, l, ans +    if len(A) == n:  # lの長さがnになったらscoreの計算をする +        score = 0 +        for i in range(q): +            a, b, c, d = l[i] +            a, b, = ( +                a - 1, +                b - 1, +            ) +            if A[b] - A[a] == c: +                score += d +        ans = max(ans, score) +    else:  # 再帰的にリストに番号を追加していく +        for i in range(u, m + 1): +            dfs(i, A + [i]) -    n, m, q = list(map(int, input().split())) -    # <方針>：Aを全列挙する -    ans = 0 -    a = [0] * q -    b = [0] * q -    c = [0] * q -    d = [0] * q -    for i in range(q): -        a[i], b[i], c[i], d[i] = list(map(int, input().split())) -        a[i] -= 1 -        b[i] -= 1 -    for A in combinations_with_replacement(list(range(1, m + 1)), n): -        tmp = 0 -        for i in range(q): -            if A[b[i]] - A[a[i]] == c[i]: -                tmp += d[i] -        ans = max(ans, tmp) +    dfs(1, [1])