# -*- coding: utf-8 -*-

# モジュールのインポート

import itertools



# 標準入力の取得

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

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

for q in range(Q):

    a_q,b_q,c_q,d_q = list(map(int, input().split()))

    a.append(a_q)

    b.append(b_q)

    c.append(c_q)

    d.append(d_q)



# 求解処理

l = list(range(1, M+1))

result = 0

for A in itertools.combinations_with_replacement(l, N):

    score = 0

    for a_i,b_i,c_i,d_i in zip(a, b, c, d):

        if A[b_i-1] - A[a_i-1] == c_i:

            score += d_i



    result = max(result, score)



# 結果出力

print(result)
# -*- coding: utf-8 -*-

# モジュールのインポート

import itertools



# 標準入力の取得

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

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

for q in range(Q):

    a_q, b_q, c_q, d_q = list(map(int, input().split()))

    a.append(a_q)

    b.append(b_q)

    c.append(c_q)

    d.append(d_q)





def get_score(A: tuple) -> int:

    """得点を計算するメソッド



    Arguments:

        A {tuple} -- 数列



    Returns:

        int -- 得点

    """

    score = 0

    for a_i, b_i, c_i, d_i in zip(a, b, c, d):

        score += (A[b_i-1] - A[a_i-1] == c_i) * d_i

    return score





def main() -> None:

    """Entry point

    """

    # 求解処理

    # 条件を満たす数列を全探索

    l = list(range(1, M+1))

    result = 0

    for A in itertools.combinations_with_replacement(l, N):

        result = max(result, get_score(A))



    # 結果出力

    print(result)





if __name__ == "__main__":

    main()

# -*- coding: utf-8 -*-
# モジュールのインポート
import itertools

# 標準入力の取得
N, M, Q = list(map(int, input().split()))
a, b, c, d = [], [], [], []
for q in range(Q):
    a_q, b_q, c_q, d_q = list(map(int, input().split()))
    a.append(a_q)
    b.append(b_q)
    c.append(c_q)
    d.append(d_q)
# 求解処理
l = list(range(1, M + 1))
result = 0
for A in itertools.combinations_with_replacement(l, N):
    score = 0
    for a_i, b_i, c_i, d_i in zip(a, b, c, d):
        if A[b_i - 1] - A[a_i - 1] == c_i:
            score += d_i
    result = max(result, score)
# 結果出力
print(result)

# -*- coding: utf-8 -*-
# モジュールのインポート
import itertools

# 標準入力の取得
N, M, Q = list(map(int, input().split()))
a, b, c, d = [], [], [], []
for q in range(Q):
    a_q, b_q, c_q, d_q = list(map(int, input().split()))
    a.append(a_q)
    b.append(b_q)
    c.append(c_q)
    d.append(d_q)


def get_score(A: tuple) -> int:
    """得点を計算するメソッド
    Arguments:
        A {tuple} -- 数列
    Returns:
        int -- 得点
    """
    score = 0
    for a_i, b_i, c_i, d_i in zip(a, b, c, d):
        score += (A[b_i - 1] - A[a_i - 1] == c_i) * d_i
    return score


def main() -> None:
    """Entry point"""
    # 求解処理
    # 条件を満たす数列を全探索
    l = list(range(1, M + 1))
    result = 0
    for A in itertools.combinations_with_replacement(l, N):
        result = max(result, get_score(A))
    # 結果出力
    print(result)


if __name__ == "__main__":
    main()

-# 求解処理 -l = list(range(1, M + 1)) -result = 0 -for A in itertools.combinations_with_replacement(l, N): + + +def get_score(A: tuple) -> int: +    """得点を計算するメソッド +    Arguments: +        A {tuple} -- 数列 +    Returns: +        int -- 得点 +    """ -        if A[b_i - 1] - A[a_i - 1] == c_i: -            score += d_i -    result = max(result, score) -# 結果出力 -print(result) +        score += (A[b_i - 1] - A[a_i - 1] == c_i) * d_i +    return score + + +def main() -> None: +    """Entry point""" +    # 求解処理 +    # 条件を満たす数列を全探索 +    l = list(range(1, M + 1)) +    result = 0 +    for A in itertools.combinations_with_replacement(l, N): +        result = max(result, get_score(A)) +    # 結果出力 +    print(result) + + +if __name__ == "__main__": +    main()