import sys



sys.setrecursionlimit(10 ** 7)

f_inf = float('inf')

mod = 10 ** 9 + 7

ans = 0





def resolve():

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

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



    A = [0] * n



    def dfs(idx):

        global ans

        if idx == n:

            score = 0

            for i in range(q):

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

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

                    score += d

            ans = max(ans, score)

        else:

            if idx == 0:

                k = 1

            else:

                k = A[idx - 1]



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

                A[idx] = i

                dfs(idx + 1)



    dfs(0)

    print(ans)





if __name__ == '__main__':

    resolve()

# https://atcoder.jp/contests/abc165/tasks/abc165_c

# C - Many Requirements

import sys



sys.setrecursionlimit(10 ** 7)

f_inf = float('inf')

mod = 10 ** 9 + 7





def resolve():

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

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



    # 数列Aのスコアを返す

    def calc_score(L):

        score = 0

        for a, b, c, d in ABCD:

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

                score += d

        return score



    # 数列Aを全パターン試す

    def dfs(A):

        if len(A) == n:

            return calc_score(A)



        res = 0

        prev = A[-1] if len(A) > 0 else 1

        for v in range(prev, m + 1):

            A.append(v)

            res = max(res, dfs(A))

            A.pop()



        return res



    print((dfs([])))





if __name__ == '__main__':

    resolve()

import sys

sys.setrecursionlimit(10**7)
f_inf = float("inf")
mod = 10**9 + 7
ans = 0


def resolve():
    n, m, q = list(map(int, input().split()))
    ABCD = [list(map(int, input().split())) for _ in range(q)]
    A = [0] * n

    def dfs(idx):
        global ans
        if idx == n:
            score = 0
            for i in range(q):
                a, b, c, d = ABCD[i]
                if A[b - 1] - A[a - 1] == c:
                    score += d
            ans = max(ans, score)
        else:
            if idx == 0:
                k = 1
            else:
                k = A[idx - 1]
            for i in range(k, m + 1):
                A[idx] = i
                dfs(idx + 1)

    dfs(0)
    print(ans)


if __name__ == "__main__":
    resolve()

# https://atcoder.jp/contests/abc165/tasks/abc165_c
# C - Many Requirements
import sys

sys.setrecursionlimit(10**7)
f_inf = float("inf")
mod = 10**9 + 7


def resolve():
    n, m, q = list(map(int, input().split()))
    ABCD = [list(map(int, input().split())) for _ in range(q)]
    # 数列Aのスコアを返す
    def calc_score(L):
        score = 0
        for a, b, c, d in ABCD:
            if L[b - 1] - L[a - 1] == c:
                score += d
        return score

    # 数列Aを全パターン試す
    def dfs(A):
        if len(A) == n:
            return calc_score(A)
        res = 0
        prev = A[-1] if len(A) > 0 else 1
        for v in range(prev, m + 1):
            A.append(v)
            res = max(res, dfs(A))
            A.pop()
        return res

    print((dfs([])))


if __name__ == "__main__":
    resolve()

+# https://atcoder.jp/contests/abc165/tasks/abc165_c +# C - Many Requirements -ans = 0 -    A = [0] * n +    # 数列Aのスコアを返す +    def calc_score(L): +        score = 0 +        for a, b, c, d in ABCD: +            if L[b - 1] - L[a - 1] == c: +                score += d +        return score -    def dfs(idx): -        global ans -        if idx == n: -            score = 0 -            for i in range(q): -                a, b, c, d = ABCD[i] -                if A[b - 1] - A[a - 1] == c: -                    score += d -            ans = max(ans, score) -        else: -            if idx == 0: -                k = 1 -            else: -                k = A[idx - 1] -            for i in range(k, m + 1): -                A[idx] = i -                dfs(idx + 1) +    # 数列Aを全パターン試す +    def dfs(A): +        if len(A) == n: +            return calc_score(A) +        res = 0 +        prev = A[-1] if len(A) > 0 else 1 +        for v in range(prev, m + 1): +            A.append(v) +            res = max(res, dfs(A)) +            A.pop() +        return res -    dfs(0) -    print(ans) +    print((dfs([])))