import itertools 

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

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



ls = list(range(n + m - 1))

mx = 0

for l in itertools.combinations(ls, m - 1):

    a = [0]

    idx = 0

    val = 1

    for i in range(n + m - 1): 

        if idx < m - 1 and i == l[idx]:

            val += 1

            idx += 1

        else: 

            a.append(val)

    #print(l, a)

    v = 0

    for i in query:

        if a[i[1]] - a[i[0]] == i[2]: v += i[3]

    mx = max(v, mx)

print(mx)

import sys

sys.setrecursionlimit(10**7)



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

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



def dfs(k, x):

    global mx

    if k == n:

        v = 0

        for line in query:

            a, b, c, d = line

            if x[b] - x[a] == c: v += d

        if mx < v: mx = v

    else:

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

            dfs(k + 1, x + [i])

    

mx = 0

dfs(0, [1])



print(mx)
import itertools

n, m, q = list(map(int, input().split()))
query = [list(map(int, input().split())) for _ in range(q)]
ls = list(range(n + m - 1))
mx = 0
for l in itertools.combinations(ls, m - 1):
    a = [0]
    idx = 0
    val = 1
    for i in range(n + m - 1):
        if idx < m - 1 and i == l[idx]:
            val += 1
            idx += 1
        else:
            a.append(val)
    # print(l, a)
    v = 0
    for i in query:
        if a[i[1]] - a[i[0]] == i[2]:
            v += i[3]
    mx = max(v, mx)
print(mx)

import sys

sys.setrecursionlimit(10**7)
n, m, q = list(map(int, input().split()))
query = [list(map(int, input().split())) for _ in range(q)]


def dfs(k, x):
    global mx
    if k == n:
        v = 0
        for line in query:
            a, b, c, d = line
            if x[b] - x[a] == c:
                v += d
        if mx < v:
            mx = v
    else:
        for i in range(x[-1], m + 1):
            dfs(k + 1, x + [i])


mx = 0
dfs(0, [1])
print(mx)

-import itertools +import sys +sys.setrecursionlimit(10**7) -ls = list(range(n + m - 1)) + + +def dfs(k, x): +    global mx +    if k == n: +        v = 0 +        for line in query: +            a, b, c, d = line +            if x[b] - x[a] == c: +                v += d +        if mx < v: +            mx = v +    else: +        for i in range(x[-1], m + 1): +            dfs(k + 1, x + [i]) + + -for l in itertools.combinations(ls, m - 1): -    a = [0] -    idx = 0 -    val = 1 -    for i in range(n + m - 1): -        if idx < m - 1 and i == l[idx]: -            val += 1 -            idx += 1 -        else: -            a.append(val) -    # print(l, a) -    v = 0 -    for i in query: -        if a[i[1]] - a[i[0]] == i[2]: -            v += i[3] -    mx = max(v, mx) +dfs(0, [1])