import itertools

n, m, q = [int(a) for a in input().split()]

a = []

b = []

c = []

d = []

for i in range(q):

    a_,b_,c_,d_ = [int(a) for a in input().split()]

    a.append(a_-1)

    b.append(b_-1)

    c.append(c_)

    d.append(d_)

ans = 0



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

    ans_ = 0

    for i in range(q):

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

            ans_ += d[i]

    if ans_ > ans:

        ans = ans_



print(ans)

import copy



def dfs(a, b, c, d, A, N, M, Q):

    ans = 0

    if len(A) == 0:

        s = 1

    else:

        s = A[-1]

    A.append(0)

    if len(A) == N:

        for v in range(s, M+1):

            A[-1] = v

            ans_q = 0

            for q in range(Q):

                if (A[b[q]] - A[a[q]]) == c[q]:

                    ans_q += d[q]

            ans = max(ans_q, ans)

    else:

        for v in range(s, M+1):

            A[-1] = v

            ans = max(dfs(a, b, c, d, copy.copy(A), N, M, Q), ans)

    return ans



N, M, Q = [int(a) for a in input().split()]

a = []

b = []

c = []

d = []

A = []



for q in range(Q):

    in_ = [int(a) for a in input().split()]

    a.append(in_[0]-1)

    b.append(in_[1]-1)

    c.append(in_[2])

    d.append(in_[3])



ans = dfs(a,b,c,d,A,N,M,Q)

print(ans)
import itertools

n, m, q = [int(a) for a in input().split()]
a = []
b = []
c = []
d = []
for i in range(q):
    a_, b_, c_, d_ = [int(a) for a in input().split()]
    a.append(a_ - 1)
    b.append(b_ - 1)
    c.append(c_)
    d.append(d_)
ans = 0
for A in itertools.combinations_with_replacement(list(range(m)), n):
    ans_ = 0
    for i in range(q):
        if A[b[i]] - A[a[i]] == c[i]:
            ans_ += d[i]
    if ans_ > ans:
        ans = ans_
print(ans)

import copy


def dfs(a, b, c, d, A, N, M, Q):
    ans = 0
    if len(A) == 0:
        s = 1
    else:
        s = A[-1]
    A.append(0)
    if len(A) == N:
        for v in range(s, M + 1):
            A[-1] = v
            ans_q = 0
            for q in range(Q):
                if (A[b[q]] - A[a[q]]) == c[q]:
                    ans_q += d[q]
            ans = max(ans_q, ans)
    else:
        for v in range(s, M + 1):
            A[-1] = v
            ans = max(dfs(a, b, c, d, copy.copy(A), N, M, Q), ans)
    return ans


N, M, Q = [int(a) for a in input().split()]
a = []
b = []
c = []
d = []
A = []
for q in range(Q):
    in_ = [int(a) for a in input().split()]
    a.append(in_[0] - 1)
    b.append(in_[1] - 1)
    c.append(in_[2])
    d.append(in_[3])
ans = dfs(a, b, c, d, A, N, M, Q)
print(ans)

-import itertools +import copy -n, m, q = [int(a) for a in input().split()] + +def dfs(a, b, c, d, A, N, M, Q): +    ans = 0 +    if len(A) == 0: +        s = 1 +    else: +        s = A[-1] +    A.append(0) +    if len(A) == N: +        for v in range(s, M + 1): +            A[-1] = v +            ans_q = 0 +            for q in range(Q): +                if (A[b[q]] - A[a[q]]) == c[q]: +                    ans_q += d[q] +            ans = max(ans_q, ans) +    else: +        for v in range(s, M + 1): +            A[-1] = v +            ans = max(dfs(a, b, c, d, copy.copy(A), N, M, Q), ans) +    return ans + + +N, M, Q = [int(a) for a in input().split()] -for i in range(q): -    a_, b_, c_, d_ = [int(a) for a in input().split()] -    a.append(a_ - 1) -    b.append(b_ - 1) -    c.append(c_) -    d.append(d_) -ans = 0 -for A in itertools.combinations_with_replacement(list(range(m)), n): -    ans_ = 0 -    for i in range(q): -        if A[b[i]] - A[a[i]] == c[i]: -            ans_ += d[i] -    if ans_ > ans: -        ans = ans_ +A = [] +for q in range(Q): +    in_ = [int(a) for a in input().split()] +    a.append(in_[0] - 1) +    b.append(in_[1] - 1) +    c.append(in_[2]) +    d.append(in_[3]) +ans = dfs(a, b, c, d, A, N, M, Q)