

import math

import itertools

import sys

import collections



def solve(N,M,Q,ABCD):

    ans = 0

    A = [i for i in range(1,M+1)]

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

        tmp = 0

        for a,b,c,d in ABCD:

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

                tmp+=d

        ans = max(ans,tmp)

    return ans







def main():

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

    ABCD = []

    for _ in range(Q):

        ABCD.append(list(map(int,input().split())))

    # solve()

    print((solve(N,M,Q,ABCD)))

if __name__ == '__main__':

   main()


import math

import itertools



def solve(N:int,M:int,Q:int,ABCD:list) -> int:

    ans = 0

    elements = [i for i in range(1,M+1)] # 数列Aにあり得る要素

    # 条件を満たす数列Aを作成

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

        total = 0 # 現在の数列の得点を格納する変数

        for a,b,c,d in ABCD:

            if(A[b-1] - A [a-1] == c): # 条件を満たしていたら得点を追加

                total+=d

        ans = max(ans,total) # 最高得点を更新

    return ans







def main():

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

    ABCD = []

    for _ in range(Q):

        ABCD.append(list(map(int,input().split())))

    # solve()

    print((solve(N,M,Q,ABCD)))

if __name__ == '__main__':

   main()
import math
import itertools
import sys
import collections


def solve(N, M, Q, ABCD):
    ans = 0
    A = [i for i in range(1, M + 1)]
    for p in itertools.combinations_with_replacement(A, N):
        tmp = 0
        for a, b, c, d in ABCD:
            if p[b - 1] - p[a - 1] == c:
                tmp += d
        ans = max(ans, tmp)
    return ans


def main():
    N, M, Q = list(map(int, input().split()))
    ABCD = []
    for _ in range(Q):
        ABCD.append(list(map(int, input().split())))
    # solve()
    print((solve(N, M, Q, ABCD)))


if __name__ == "__main__":
    main()

import math
import itertools


def solve(N: int, M: int, Q: int, ABCD: list) -> int:
    ans = 0
    elements = [i for i in range(1, M + 1)]  # 数列Aにあり得る要素
    # 条件を満たす数列Aを作成
    for A in itertools.combinations_with_replacement(elements, N):
        total = 0  # 現在の数列の得点を格納する変数
        for a, b, c, d in ABCD:
            if A[b - 1] - A[a - 1] == c:  # 条件を満たしていたら得点を追加
                total += d
        ans = max(ans, total)  # 最高得点を更新
    return ans


def main():
    N, M, Q = list(map(int, input().split()))
    ABCD = []
    for _ in range(Q):
        ABCD.append(list(map(int, input().split())))
    # solve()
    print((solve(N, M, Q, ABCD)))


if __name__ == "__main__":
    main()

-import sys -import collections -def solve(N, M, Q, ABCD): +def solve(N: int, M: int, Q: int, ABCD: list) -> int: -    A = [i for i in range(1, M + 1)] -    for p in itertools.combinations_with_replacement(A, N): -        tmp = 0 +    elements = [i for i in range(1, M + 1)]  # 数列Aにあり得る要素 +    # 条件を満たす数列Aを作成 +    for A in itertools.combinations_with_replacement(elements, N): +        total = 0  # 現在の数列の得点を格納する変数 -            if p[b - 1] - p[a - 1] == c: -                tmp += d -        ans = max(ans, tmp) +            if A[b - 1] - A[a - 1] == c:  # 条件を満たしていたら得点を追加 +                total += d +        ans = max(ans, total)  # 最高得点を更新