import itertools



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

l=[i for i in range(1,m+1)]

que=list(itertools.combinations_with_replacement(l, n))

point=[0 for i in range(len(que))]

ans=0



for i in range(q):

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

  a-=1

  b-=1

  for j in range(len(que)):

    if que[j][b]-que[j][a]==c:

      point[j]+=d

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

a,b,c,d = [],[],[],[]

ans = 0

A = [1]



for i in range(q):

    ai,bi,ci,di = list(map(int, input().split()))

    a.append(ai)

    b.append(bi)

    c.append(ci)

    d.append(di)



def dfs(A):

    global ans

    if n == len(A):

        now = 0

        for i in range(q):

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

                now += d[i]

        ans  = max(ans,now)

        return



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

        A.append(i)

        dfs(A)

        A.pop() #最後の要素を削除

dfs(A)

print(ans)

import itertools

n, m, q = list(map(int, input().split()))
l = [i for i in range(1, m + 1)]
que = list(itertools.combinations_with_replacement(l, n))
point = [0 for i in range(len(que))]
ans = 0
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    a -= 1
    b -= 1
    for j in range(len(que)):
        if que[j][b] - que[j][a] == c:
            point[j] += d
print((max(point)))

n, m, q = list(map(int, input().split()))
a, b, c, d = [], [], [], []
ans = 0
A = [1]
for i in range(q):
    ai, bi, ci, di = list(map(int, input().split()))
    a.append(ai)
    b.append(bi)
    c.append(ci)
    d.append(di)


def dfs(A):
    global ans
    if n == len(A):
        now = 0
        for i in range(q):
            if A[b[i] - 1] - A[a[i] - 1] == c[i]:
                now += d[i]
        ans = max(ans, now)
        return
    for i in range(A[-1], m + 1):
        A.append(i)
        dfs(A)
        A.pop()  # 最後の要素を削除


dfs(A)
print(ans)

-import itertools +n, m, q = list(map(int, input().split())) +a, b, c, d = [], [], [], [] +ans = 0 +A = [1] +for i in range(q): +    ai, bi, ci, di = list(map(int, input().split())) +    a.append(ai) +    b.append(bi) +    c.append(ci) +    d.append(di) -n, m, q = list(map(int, input().split())) -l = [i for i in range(1, m + 1)] -que = list(itertools.combinations_with_replacement(l, n)) -point = [0 for i in range(len(que))] -ans = 0 -for i in range(q): -    a, b, c, d = list(map(int, input().split())) -    a -= 1 -    b -= 1 -    for j in range(len(que)): -        if que[j][b] - que[j][a] == c: -            point[j] += d -print((max(point))) + +def dfs(A): +    global ans +    if n == len(A): +        now = 0 +        for i in range(q): +            if A[b[i] - 1] - A[a[i] - 1] == c[i]: +                now += d[i] +        ans = max(ans, now) +        return +    for i in range(A[-1], m + 1): +        A.append(i) +        dfs(A) +        A.pop()  # 最後の要素を削除 + + +dfs(A) +print(ans)