import sys

sys.setrecursionlimit(2147483647)

INF=float("inf")

MOD=10**9+7 # 998244353

input=lambda:sys.stdin.readline().rstrip()

def resolve():

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

    ABCD = [None] * q

    for i in range(q):

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

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



    def dfs(A):

        # base case

        if len(A) == n + 1: # score を計算して返す

            # print(A)

            score = 0

            for a, b, c, d in ABCD:

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

                    score += d

            return score

        # iterate

        res = -INF

        for i in range(A[-1], m + 1):

            res = max(res, dfs(A + [i]))

        return res



    print((dfs([1])))

resolve()
import sys

INF = 1 << 60

MOD = 10**9 + 7 # 998244353

sys.setrecursionlimit(2147483647)

input = lambda:sys.stdin.readline().rstrip()

from itertools import combinations_with_replacement

def resolve():

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

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

    ans = 0

    for I in combinations_with_replacement(list(range(m)), n):

        score = 0

        for a, b, c, d in ABCD:

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

                score += d

        ans = max(ans, score)

    print(ans)

resolve()
import sys

sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7  # 998244353
input = lambda: sys.stdin.readline().rstrip()


def resolve():
    n, m, q = list(map(int, input().split()))
    ABCD = [None] * q
    for i in range(q):
        a, b, c, d = list(map(int, input().split()))
        ABCD[i] = (a, b, c, d)

    def dfs(A):
        # base case
        if len(A) == n + 1:  # score を計算して返す
            # print(A)
            score = 0
            for a, b, c, d in ABCD:
                if A[b] - A[a] == c:
                    score += d
            return score
        # iterate
        res = -INF
        for i in range(A[-1], m + 1):
            res = max(res, dfs(A + [i]))
        return res

    print((dfs([1])))


resolve()

import sys

INF = 1 << 60
MOD = 10**9 + 7  # 998244353
sys.setrecursionlimit(2147483647)
input = lambda: sys.stdin.readline().rstrip()
from itertools import combinations_with_replacement


def resolve():
    n, m, q = list(map(int, input().split()))
    ABCD = [tuple(map(int, input().split())) for _ in range(q)]
    ans = 0
    for I in combinations_with_replacement(list(range(m)), n):
        score = 0
        for a, b, c, d in ABCD:
            if I[b - 1] - I[a - 1] == c:
                score += d
        ans = max(ans, score)
    print(ans)


resolve()

+INF = 1 << 60 +MOD = 10**9 + 7  # 998244353 -INF = float("inf") -MOD = 10**9 + 7  # 998244353 +from itertools import combinations_with_replacement -    ABCD = [None] * q -    for i in range(q): -        a, b, c, d = list(map(int, input().split())) -        ABCD[i] = (a, b, c, d) - -    def dfs(A): -        # base case -        if len(A) == n + 1:  # score を計算して返す -            # print(A) -            score = 0 -            for a, b, c, d in ABCD: -                if A[b] - A[a] == c: -                    score += d -            return score -        # iterate -        res = -INF -        for i in range(A[-1], m + 1): -            res = max(res, dfs(A + [i])) -        return res - -    print((dfs([1]))) +    ABCD = [tuple(map(int, input().split())) for _ in range(q)] +    ans = 0 +    for I in combinations_with_replacement(list(range(m)), n): +        score = 0 +        for a, b, c, d in ABCD: +            if I[b - 1] - I[a - 1] == c: +                score += d +        ans = max(ans, score) +    print(ans)