import sys

readline = sys.stdin.readline

from collections import deque



def main():

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

    P = [tuple(map(int, readline().rstrip().split())) for _ in range(Q)]

    ans = 0

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

    que = deque(l)

    for i in range(N):

        for _ in range(len(que)):

            ll = que.popleft()

            for j in range(ll[-1], M+1):

                que.append(ll + [j])

    

    for pair in que:

        score = 0

        for a, b, c, d in P:

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

                score += d

        ans = max(ans, score)

    

    print(ans)



if __name__ == '__main__':

    main()
import sys

readline = sys.stdin.readline

from itertools import combinations_with_replacement



def main():

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

    P = [tuple(map(int, readline().rstrip().split())) for _ in range(Q)]

    ans = 0    

    for pair in combinations_with_replacement([i for i in range(1, M+1)], N):

        score = 0

        for a, b, c, d in P:

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

                score += d

        ans = max(ans, score)

    

    print(ans)



if __name__ == '__main__':

    main()
import sys

readline = sys.stdin.readline
from collections import deque


def main():
    N, M, Q = list(map(int, readline().rstrip().split()))
    P = [tuple(map(int, readline().rstrip().split())) for _ in range(Q)]
    ans = 0
    l = [[i] for i in range(1, M + 1)]
    que = deque(l)
    for i in range(N):
        for _ in range(len(que)):
            ll = que.popleft()
            for j in range(ll[-1], M + 1):
                que.append(ll + [j])
    for pair in que:
        score = 0
        for a, b, c, d in P:
            if pair[b - 1] - pair[a - 1] == c:
                score += d
        ans = max(ans, score)
    print(ans)


if __name__ == "__main__":
    main()

import sys

readline = sys.stdin.readline
from itertools import combinations_with_replacement


def main():
    N, M, Q = list(map(int, readline().rstrip().split()))
    P = [tuple(map(int, readline().rstrip().split())) for _ in range(Q)]
    ans = 0
    for pair in combinations_with_replacement([i for i in range(1, M + 1)], N):
        score = 0
        for a, b, c, d in P:
            if pair[b - 1] - pair[a - 1] == c:
                score += d
        ans = max(ans, score)
    print(ans)


if __name__ == "__main__":
    main()

-from collections import deque +from itertools import combinations_with_replacement -    l = [[i] for i in range(1, M + 1)] -    que = deque(l) -    for i in range(N): -        for _ in range(len(que)): -            ll = que.popleft() -            for j in range(ll[-1], M + 1): -                que.append(ll + [j]) -    for pair in que: +    for pair in combinations_with_replacement([i for i in range(1, M + 1)], N):