from sys import stdin





def input():

    return stdin.readline().strip()





def main():

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

    a, b, c, d = [0] * Q, [0] * Q, [0] * Q, [0] * Q

    for i in range(Q):

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

        a[i] -= 1

        b[i] -= 1



    ans = 0



    def dfs(seq):

        nonlocal ans

        if len(seq) == N:

            score = 0

            for i in range(Q):

                if seq[b[i]] - seq[a[i]] == c[i]:

                    score += d[i]

            ans = max(ans, score)

            return



        score = 0

        start = seq[-1] if seq else 1

        for n in range(start, M + 1):

            seq.append(n)

            dfs(seq)

            seq.pop()



    dfs([])

    print(ans)





if __name__ == "__main__":

    main()

from sys import stdin





def input():

    return stdin.readline().strip()





def main():

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

    a, b, c, d = [0] * Q, [0] * Q, [0] * Q, [0] * Q

    for i in range(Q):

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

        a[i] -= 1

        b[i] -= 1



    ans = 0



    def dfs(seq):

        nonlocal ans

        if len(seq) == N:

            score = 0

            for i in range(Q):

                if seq[b[i]] - seq[a[i]] == c[i]:

                    score += d[i]

            ans = max(ans, score)

            return



        score = 0

        start = seq[-1] if seq else 1

        for n in range(start, M + 1):

            seq.append(n)

            dfs(seq)

            seq.pop()



    dfs([1])

    print(ans)





if __name__ == "__main__":

    main()

from sys import stdin


def input():
    return stdin.readline().strip()


def main():
    N, M, Q = list(map(int, input().split()))
    a, b, c, d = [0] * Q, [0] * Q, [0] * Q, [0] * Q
    for i in range(Q):
        a[i], b[i], c[i], d[i] = list(map(int, input().split()))
        a[i] -= 1
        b[i] -= 1
    ans = 0

    def dfs(seq):
        nonlocal ans
        if len(seq) == N:
            score = 0
            for i in range(Q):
                if seq[b[i]] - seq[a[i]] == c[i]:
                    score += d[i]
            ans = max(ans, score)
            return
        score = 0
        start = seq[-1] if seq else 1
        for n in range(start, M + 1):
            seq.append(n)
            dfs(seq)
            seq.pop()

    dfs([])
    print(ans)


if __name__ == "__main__":
    main()

from sys import stdin


def input():
    return stdin.readline().strip()


def main():
    N, M, Q = list(map(int, input().split()))
    a, b, c, d = [0] * Q, [0] * Q, [0] * Q, [0] * Q
    for i in range(Q):
        a[i], b[i], c[i], d[i] = list(map(int, input().split()))
        a[i] -= 1
        b[i] -= 1
    ans = 0

    def dfs(seq):
        nonlocal ans
        if len(seq) == N:
            score = 0
            for i in range(Q):
                if seq[b[i]] - seq[a[i]] == c[i]:
                    score += d[i]
            ans = max(ans, score)
            return
        score = 0
        start = seq[-1] if seq else 1
        for n in range(start, M + 1):
            seq.append(n)
            dfs(seq)
            seq.pop()

    dfs([1])
    print(ans)


if __name__ == "__main__":
    main()

-    dfs([]) +    dfs([1])