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

Q=[list(map(int,input().split())) for i in range(q)]

ans=0

#めっちゃfor文あるならdfs

R=[list(range(i,m)) for i in range(m)]

def dfs(d,s,l):

    global n,m,Q,ans

    if d==n:

        ans_=0

        for k in Q:

            a,b,c,e=k

            if int(s[b-1])-int(s[a-1])==c:

                ans_+=e

        ans=max(ans,ans_)

    else:

        for i in R[l]:

            dfs(d+1,s+str(i),i)

dfs(0,"",0)



print(ans)
from itertools import combinations_with_replacement

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

Q=[list(map(int,input().split())) for i in range(q)]

ans=0

for s in combinations_with_replacement(list(range(1,m+1)),n):

    ans_=0

    for k in Q:

        a,b,c,d=k

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

            ans_+=d

    ans=max(ans_,ans)

print(ans)
n, m, q = list(map(int, input().split()))
Q = [list(map(int, input().split())) for i in range(q)]
ans = 0
# めっちゃfor文あるならdfs
R = [list(range(i, m)) for i in range(m)]


def dfs(d, s, l):
    global n, m, Q, ans
    if d == n:
        ans_ = 0
        for k in Q:
            a, b, c, e = k
            if int(s[b - 1]) - int(s[a - 1]) == c:
                ans_ += e
        ans = max(ans, ans_)
    else:
        for i in R[l]:
            dfs(d + 1, s + str(i), i)


dfs(0, "", 0)
print(ans)

from itertools import combinations_with_replacement

n, m, q = list(map(int, input().split()))
Q = [list(map(int, input().split())) for i in range(q)]
ans = 0
for s in combinations_with_replacement(list(range(1, m + 1)), n):
    ans_ = 0
    for k in Q:
        a, b, c, d = k
        if s[b - 1] - s[a - 1] == c:
            ans_ += d
    ans = max(ans_, ans)
print(ans)

+from itertools import combinations_with_replacement + -# めっちゃfor文あるならdfs -R = [list(range(i, m)) for i in range(m)] - - -def dfs(d, s, l): -    global n, m, Q, ans -    if d == n: -        ans_ = 0 -        for k in Q: -            a, b, c, e = k -            if int(s[b - 1]) - int(s[a - 1]) == c: -                ans_ += e -        ans = max(ans, ans_) -    else: -        for i in R[l]: -            dfs(d + 1, s + str(i), i) - - -dfs(0, "", 0) +for s in combinations_with_replacement(list(range(1, m + 1)), n): +    ans_ = 0 +    for k in Q: +        a, b, c, d = k +        if s[b - 1] - s[a - 1] == c: +            ans_ += d +    ans = max(ans_, ans)