#!/usr/bin/env python3

import sys

import functools

sys.setrecursionlimit(10**8)

INF = float("inf")





def solve(N: int, M: int, Q: int,

          a: "List[int]", b: "List[int]", c: "List[int]", d: "List[int]"):



    # 全列挙行けるかを確認

    @functools.lru_cache(maxsize=None)

    def f(n, m):

        if n == 1:

            return m

        ret = 0

        for i in range(m, 0, -1):

            ret += f(n-1, i)

        return ret

    # print(f(N, M))



    samples = []

    # 全列挙



    def rec(curr, lower, d):

        # d: 今の深さ

        if d < N:

            for i in range(lower, M+1):

                rec(curr+[i], i, d+1)

        else:

            for i in range(lower, M+1):

                samples.append(curr+[i])



    max_tot = -INF

    rec([], 1, 1)



    # print(len(samples))

    for A in samples:  # 10^5程度のループ

        # print(A)

        tot = 0

        for aa, bb, cc, dd in zip(a, b, c, d):

            if A[bb-1] - A[aa-1] == cc:

                tot += dd

        max_tot = max(tot, max_tot)

    print(max_tot)

    return





def main():



    def iterate_tokens():

        for line in sys.stdin:

            for word in line.split():

                yield word

    tokens = iterate_tokens()

    N = int(next(tokens))  # type: int

    M = int(next(tokens))  # type: int

    Q = int(next(tokens))  # type: int

    a = [int()] * (Q)  # type: "List[int]"

    b = [int()] * (Q)  # type: "List[int]"

    c = [int()] * (Q)  # type: "List[int]"

    d = [int()] * (Q)  # type: "List[int]"

    for i in range(Q):

        a[i] = int(next(tokens))

        b[i] = int(next(tokens))

        c[i] = int(next(tokens))

        d[i] = int(next(tokens))

    solve(N, M, Q, a, b, c, d)





if __name__ == '__main__':

    main()

#!/usr/bin/env python3

import sys

sys.setrecursionlimit(10**8)

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

def INT(): return int(eval(input()))

def MAP(): return list(map(int, input().split()))

def LIST(): return list(map(int, input().split()))

def ZIP(n): return [LIST() for _ in range(n)]





INF = float("inf")

MOD = 10**9 + 7





def main():

    n, m, q = MAP()

    abcd = ZIP(q)



    def ascending(X):

        for i in range(1, n):

            if A[i - 1] > A[i]:

                return False

        return True



    def point(A):

        tot = 0

        for a, b, c, d in abcd:

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

                tot += d

        return tot



    def forward(A):

        # Mより小さい最後の桁

        k = None

        for i in reversed(list(range(n))):

            if A[i] < m:

                k = i

                break

        if k is not None:

            A[k] += 1

            for j in range(k + 1, n):

                A[j] = A[k]

        else:

            A[0] = INF



    # 昇順列を巡回する

    A = [1] * n

    ans = 0

    while ascending(A):         # O(N)

        # print(A)

        p = point(A)

        if ans <= p:

            ans = p

        forward(A)

    print(ans)



    return





if __name__ == '__main__':

    main()

#!/usr/bin/env python3
import sys
import functools

sys.setrecursionlimit(10**8)
INF = float("inf")


def solve(
    N: int,
    M: int,
    Q: int,
    a: "List[int]",
    b: "List[int]",
    c: "List[int]",
    d: "List[int]",
):
    # 全列挙行けるかを確認
    @functools.lru_cache(maxsize=None)
    def f(n, m):
        if n == 1:
            return m
        ret = 0
        for i in range(m, 0, -1):
            ret += f(n - 1, i)
        return ret

    # print(f(N, M))
    samples = []
    # 全列挙
    def rec(curr, lower, d):
        # d: 今の深さ
        if d < N:
            for i in range(lower, M + 1):
                rec(curr + [i], i, d + 1)
        else:
            for i in range(lower, M + 1):
                samples.append(curr + [i])

    max_tot = -INF
    rec([], 1, 1)
    # print(len(samples))
    for A in samples:  # 10^5程度のループ
        # print(A)
        tot = 0
        for aa, bb, cc, dd in zip(a, b, c, d):
            if A[bb - 1] - A[aa - 1] == cc:
                tot += dd
        max_tot = max(tot, max_tot)
    print(max_tot)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (Q)  # type: "List[int]"
    b = [int()] * (Q)  # type: "List[int]"
    c = [int()] * (Q)  # type: "List[int]"
    d = [int()] * (Q)  # type: "List[int]"
    for i in range(Q):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
        c[i] = int(next(tokens))
        d[i] = int(next(tokens))
    solve(N, M, Q, a, b, c, d)


if __name__ == "__main__":
    main()

#!/usr/bin/env python3
import sys

sys.setrecursionlimit(10**8)


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


def INT():
    return int(eval(input()))


def MAP():
    return list(map(int, input().split()))


def LIST():
    return list(map(int, input().split()))


def ZIP(n):
    return [LIST() for _ in range(n)]


INF = float("inf")
MOD = 10**9 + 7


def main():
    n, m, q = MAP()
    abcd = ZIP(q)

    def ascending(X):
        for i in range(1, n):
            if A[i - 1] > A[i]:
                return False
        return True

    def point(A):
        tot = 0
        for a, b, c, d in abcd:
            if A[b - 1] - A[a - 1] == c:
                tot += d
        return tot

    def forward(A):
        # Mより小さい最後の桁
        k = None
        for i in reversed(list(range(n))):
            if A[i] < m:
                k = i
                break
        if k is not None:
            A[k] += 1
            for j in range(k + 1, n):
                A[j] = A[k]
        else:
            A[0] = INF

    # 昇順列を巡回する
    A = [1] * n
    ans = 0
    while ascending(A):  # O(N)
        # print(A)
        p = point(A)
        if ans <= p:
            ans = p
        forward(A)
    print(ans)
    return


if __name__ == "__main__":
    main()

-import functools -INF = float("inf") -def solve( -    N: int, -    M: int, -    Q: int, -    a: "List[int]", -    b: "List[int]", -    c: "List[int]", -    d: "List[int]", -): -    # 全列挙行けるかを確認 -    @functools.lru_cache(maxsize=None) -    def f(n, m): -        if n == 1: -            return m -        ret = 0 -        for i in range(m, 0, -1): -            ret += f(n - 1, i) -        return ret +def input(): +    return sys.stdin.readline().strip() -    # print(f(N, M)) -    samples = [] -    # 全列挙 -    def rec(curr, lower, d): -        # d: 今の深さ -        if d < N: -            for i in range(lower, M + 1): -                rec(curr + [i], i, d + 1) -        else: -            for i in range(lower, M + 1): -                samples.append(curr + [i]) -    max_tot = -INF -    rec([], 1, 1) -    # print(len(samples)) -    for A in samples:  # 10^5程度のループ -        # print(A) -        tot = 0 -        for aa, bb, cc, dd in zip(a, b, c, d): -            if A[bb - 1] - A[aa - 1] == cc: -                tot += dd -        max_tot = max(tot, max_tot) -    print(max_tot) -    return +def INT(): +    return int(eval(input())) + + +def MAP(): +    return list(map(int, input().split())) + + +def LIST(): +    return list(map(int, input().split())) + + +def ZIP(n): +    return [LIST() for _ in range(n)] + + +INF = float("inf") +MOD = 10**9 + 7 -    def iterate_tokens(): -        for line in sys.stdin: -            for word in line.split(): -                yield word +    n, m, q = MAP() +    abcd = ZIP(q) -    tokens = iterate_tokens() -    N = int(next(tokens))  # type: int -    M = int(next(tokens))  # type: int -    Q = int(next(tokens))  # type: int -    a = [int()] * (Q)  # type: "List[int]" -    b = [int()] * (Q)  # type: "List[int]" -    c = [int()] * (Q)  # type: "List[int]" -    d = [int()] * (Q)  # type: "List[int]" -    for i in range(Q): -        a[i] = int(next(tokens)) -        b[i] = int(next(tokens)) -        c[i] = int(next(tokens)) -        d[i] = int(next(tokens)) -    solve(N, M, Q, a, b, c, d) +    def ascending(X): +        for i in range(1, n): +            if A[i - 1] > A[i]: +                return False +        return True + +    def point(A): +        tot = 0 +        for a, b, c, d in abcd: +            if A[b - 1] - A[a - 1] == c: +                tot += d +        return tot + +    def forward(A): +        # Mより小さい最後の桁 +        k = None +        for i in reversed(list(range(n))): +            if A[i] < m: +                k = i +                break +        if k is not None: +            A[k] += 1 +            for j in range(k + 1, n): +                A[j] = A[k] +        else: +            A[0] = INF + +    # 昇順列を巡回する +    A = [1] * n +    ans = 0 +    while ascending(A):  # O(N) +        # print(A) +        p = point(A) +        if ans <= p: +            ans = p +        forward(A) +    print(ans) +    return