from itertools import *

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

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

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

ans = 0

for l in combinations_with_replacement(A, n):

  l = list(l)

  tmp = 0

  for a,b,c,d in lst:

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

          tmp += d

  ans = max(ans, tmp)

print(ans)
def dfs(nums, length, min_lim):

    ans = 0

    if length == n:

        score_ret = 0

        for a,b,c,d in req:

            if nums[b] - nums[a] == c:

                score_ret += d

        return score_ret

    

    else:

        for nu in range(min_lim, m+1):

            new_nums = nums + [nu]

            score = dfs(new_nums, length+1, nu)

            ans = max(ans, score)

    return ans



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

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

ans = dfs([-1], 0, 1)

print(ans)
from itertools import *

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

def dfs(nums, length, min_lim):
    ans = 0
    if length == n:
        score_ret = 0
        for a, b, c, d in req:
            if nums[b] - nums[a] == c:
                score_ret += d
        return score_ret
    else:
        for nu in range(min_lim, m + 1):
            new_nums = nums + [nu]
            score = dfs(new_nums, length + 1, nu)
            ans = max(ans, score)
    return ans


n, m, q = list(map(int, input().split()))
req = [list(map(int, input().split())) for _ in range(q)]
ans = dfs([-1], 0, 1)
print(ans)

-from itertools import * +def dfs(nums, length, min_lim): +    ans = 0 +    if length == n: +        score_ret = 0 +        for a, b, c, d in req: +            if nums[b] - nums[a] == c: +                score_ret += d +        return score_ret +    else: +        for nu in range(min_lim, m + 1): +            new_nums = nums + [nu] +            score = dfs(new_nums, length + 1, nu) +            ans = max(ans, score) +    return ans + -lst = [list(map(int, input().split())) for _ in range(q)] -A = [i for i in range(1, m + 1)] -ans = 0 -for l in combinations_with_replacement(A, n): -    l = list(l) -    tmp = 0 -    for a, b, c, d in lst: -        if l[b - 1] - l[a - 1] == c: -            tmp += d -    ans = max(ans, tmp) +req = [list(map(int, input().split())) for _ in range(q)] +ans = dfs([-1], 0, 1)