def main():

    import itertools

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

    ops = []

    for i in range(q):

        ops.append(list(map(int,input().split(" "))))

    

    ans = 0

    for A in itertools.combinations_with_replacement(list(range(1,m+1)),n):

        temp = 0

        for op in ops:

            if A[op[1]-1] - A[op[0]-1] == op[2]:

                temp+=op[3]

        ans = max(ans,temp)

    

    print(ans)

main()
def score(A,g):

    score = 0

    for p in g:

        if A[p[1]]-A[p[0]]==p[2]:

            score+=p[3]

    return score



def solve(m,n,g,A,dep):

    if dep==n:

        return score(A,g)

    else:

        temp_scores = []

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

            A[dep+1] = i

            temp_scores.append(solve(m,n,g,A,dep+1))

        return max(temp_scores)



def main():

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

    g = []

    for _ in range(q):

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

        g.append((a,b,c,d))

    A = [0]*(n+1)

    A[0]=1

    print((solve(m,n,g,A,0)))

main()
def main():
    import itertools

    n, m, q = list(map(int, input().split(" ")))
    ops = []
    for i in range(q):
        ops.append(list(map(int, input().split(" "))))
    ans = 0
    for A in itertools.combinations_with_replacement(list(range(1, m + 1)), n):
        temp = 0
        for op in ops:
            if A[op[1] - 1] - A[op[0] - 1] == op[2]:
                temp += op[3]
        ans = max(ans, temp)
    print(ans)


main()

def score(A, g):
    score = 0
    for p in g:
        if A[p[1]] - A[p[0]] == p[2]:
            score += p[3]
    return score


def solve(m, n, g, A, dep):
    if dep == n:
        return score(A, g)
    else:
        temp_scores = []
        for i in range(A[dep], m + 1):
            A[dep + 1] = i
            temp_scores.append(solve(m, n, g, A, dep + 1))
        return max(temp_scores)


def main():
    n, m, q = list(map(int, input().split()))
    g = []
    for _ in range(q):
        a, b, c, d = list(map(int, input().split()))
        g.append((a, b, c, d))
    A = [0] * (n + 1)
    A[0] = 1
    print((solve(m, n, g, A, 0)))


main()

+def score(A, g): +    score = 0 +    for p in g: +        if A[p[1]] - A[p[0]] == p[2]: +            score += p[3] +    return score + + +def solve(m, n, g, A, dep): +    if dep == n: +        return score(A, g) +    else: +        temp_scores = [] +        for i in range(A[dep], m + 1): +            A[dep + 1] = i +            temp_scores.append(solve(m, n, g, A, dep + 1)) +        return max(temp_scores) + + -    import itertools - -    n, m, q = list(map(int, input().split(" "))) -    ops = [] -    for i in range(q): -        ops.append(list(map(int, input().split(" ")))) -    ans = 0 -    for A in itertools.combinations_with_replacement(list(range(1, m + 1)), n): -        temp = 0 -        for op in ops: -            if A[op[1] - 1] - A[op[0] - 1] == op[2]: -                temp += op[3] -        ans = max(ans, temp) -    print(ans) +    n, m, q = list(map(int, input().split())) +    g = [] +    for _ in range(q): +        a, b, c, d = list(map(int, input().split())) +        g.append((a, b, c, d)) +    A = [0] * (n + 1) +    A[0] = 1 +    print((solve(m, n, g, A, 0)))