import sys

from itertools import combinations_with_replacement



input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)





def main():

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

    abcd = [None] * Q

    for i in range(Q):

        abcd[i] = tuple(map(int, input().split()))



    ans = 0

    for A in combinations_with_replacement(list(range(1, M+1)), N):

        score = 0

        for q in abcd:

            a, b, c, d = q

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

                score += d

        if score > ans:

            ans = score



    print(ans)





if __name__ == "__main__":

    main()

import sys

from itertools import combinations_with_replacement



input = sys.stdin.readline





def main():

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

    abcd = [None] * Q

    for i in range(Q):

        abcd[i] = list(map(int, input().split()))

        abcd[i][0] -= 1

        abcd[i][1] -= 1



    ans = 0

    for A in combinations_with_replacement(list(range(1, M + 1)), N):

        score = 0

        for a, b, c, d in abcd:

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

                score += d

        if score > ans:

            ans = score



    print(ans)





if __name__ == "__main__":

    main()

import sys
from itertools import combinations_with_replacement

input = sys.stdin.readline
sys.setrecursionlimit(10**7)


def main():
    N, M, Q = list(map(int, input().split()))
    abcd = [None] * Q
    for i in range(Q):
        abcd[i] = tuple(map(int, input().split()))
    ans = 0
    for A in combinations_with_replacement(list(range(1, M + 1)), N):
        score = 0
        for q in abcd:
            a, b, c, d = q
            if A[b - 1] - A[a - 1] == c:
                score += d
        if score > ans:
            ans = score
    print(ans)


if __name__ == "__main__":
    main()

import sys
from itertools import combinations_with_replacement

input = sys.stdin.readline


def main():
    N, M, Q = list(map(int, input().split()))
    abcd = [None] * Q
    for i in range(Q):
        abcd[i] = list(map(int, input().split()))
        abcd[i][0] -= 1
        abcd[i][1] -= 1
    ans = 0
    for A in combinations_with_replacement(list(range(1, M + 1)), N):
        score = 0
        for a, b, c, d in abcd:
            if A[b] - A[a] == c:
                score += d
        if score > ans:
            ans = score
    print(ans)


if __name__ == "__main__":
    main()

-sys.setrecursionlimit(10**7) -        abcd[i] = tuple(map(int, input().split())) +        abcd[i] = list(map(int, input().split())) +        abcd[i][0] -= 1 +        abcd[i][1] -= 1 -        for q in abcd: -            a, b, c, d = q -            if A[b - 1] - A[a - 1] == c: +        for a, b, c, d in abcd: +            if A[b] - A[a] == c: