import itertools

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

a=[0 for i in range(q)]

b=[0 for i in range(q)]

c=[0 for i in range(q)]

d=[0 for i in range(q)]

for i in range(q):

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

    a[i]-=1

    b[i]-=1

ans=0



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

    tmp=0

    for j in range(q):

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

            tmp+=d[j]

    ans=max(ans,tmp)

print(ans)
import itertools

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

a=[0 for i in range(q)]

b=[0 for i in range(q)]

c=[0 for i in range(q)]

d=[0 for i in range(q)]

for i in range(q):

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

    a[i]-=1

    b[i]-=1

ans=0

buf=[None]*n



def dfs(nowdigit,cannum):

    global ans

    if nowdigit==n:

        tmp=0

        for j in range(q):

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

                tmp+=d[j]

        ans=max(ans,tmp)

    else:

        for i in range(cannum,m+1):

            buf[nowdigit]=i

            dfs(nowdigit+1,i)

dfs(0,1)

print(ans)
import itertools

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

import itertools

n, m, q = list(map(int, input().split()))
a = [0 for i in range(q)]
b = [0 for i in range(q)]
c = [0 for i in range(q)]
d = [0 for i in range(q)]
for i in range(q):
    a[i], b[i], c[i], d[i] = list(map(int, input().split()))
    a[i] -= 1
    b[i] -= 1
ans = 0
buf = [None] * n


def dfs(nowdigit, cannum):
    global ans
    if nowdigit == n:
        tmp = 0
        for j in range(q):
            if buf[b[j]] - buf[a[j]] == c[j]:
                tmp += d[j]
        ans = max(ans, tmp)
    else:
        for i in range(cannum, m + 1):
            buf[nowdigit] = i
            dfs(nowdigit + 1, i)


dfs(0, 1)
print(ans)

-for i in itertools.combinations_with_replacement(list(range(1, m + 1)), n): -    tmp = 0 -    for j in range(q): -        if i[b[j]] - i[a[j]] == c[j]: -            tmp += d[j] -    ans = max(ans, tmp) +buf = [None] * n + + +def dfs(nowdigit, cannum): +    global ans +    if nowdigit == n: +        tmp = 0 +        for j in range(q): +            if buf[b[j]] - buf[a[j]] == c[j]: +                tmp += d[j] +        ans = max(ans, tmp) +    else: +        for i in range(cannum, m + 1): +            buf[nowdigit] = i +            dfs(nowdigit + 1, i) + + +dfs(0, 1)