import sys

sys.setrecursionlimit(10 ** 9)



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

abcd = []

for i in range(q):

  abcd.append(tuple(map(int,input().split())))



ans = 0

def dfs(l):

  global ans

  if len(l) == n:

    ans = max(check(l),ans)

  else:

    if len(l) == 0:

      mx = 1

    else:

      mx = l[-1]

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

      tmpl = l + [i]

      dfs(tmpl)



def check(l):

  res = 0

  for a,b,c,d in abcd:

    a -= 1

    b -= 1

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

      res += d

  return res



dfs([])

print(ans)

import itertools



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

abcd = []

for i in range(q):

  abcd.append(tuple(map(int,input().split())))



def check(l):

  res = 0

  for a,b,c,d in abcd:

    a -= 1

    b -= 1

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

      res += d

  return res



l = itertools.combinations_with_replacement(list(range(1,m+1)),n)

ans = 0

for li in l:

  ans = max(check(li),ans)

print(ans)

import sys

sys.setrecursionlimit(10**9)
n, m, q = list(map(int, input().split()))
abcd = []
for i in range(q):
    abcd.append(tuple(map(int, input().split())))
ans = 0


def dfs(l):
    global ans
    if len(l) == n:
        ans = max(check(l), ans)
    else:
        if len(l) == 0:
            mx = 1
        else:
            mx = l[-1]
        for i in range(mx, m + 1):
            tmpl = l + [i]
            dfs(tmpl)


def check(l):
    res = 0
    for a, b, c, d in abcd:
        a -= 1
        b -= 1
        if l[b] - l[a] == c:
            res += d
    return res


dfs([])
print(ans)

import itertools

n, m, q = list(map(int, input().split()))
abcd = []
for i in range(q):
    abcd.append(tuple(map(int, input().split())))


def check(l):
    res = 0
    for a, b, c, d in abcd:
        a -= 1
        b -= 1
        if l[b] - l[a] == c:
            res += d
    return res


l = itertools.combinations_with_replacement(list(range(1, m + 1)), n)
ans = 0
for li in l:
    ans = max(check(li), ans)
print(ans)

-import sys +import itertools -sys.setrecursionlimit(10**9) -ans = 0 - - -def dfs(l): -    global ans -    if len(l) == n: -        ans = max(check(l), ans) -    else: -        if len(l) == 0: -            mx = 1 -        else: -            mx = l[-1] -        for i in range(mx, m + 1): -            tmpl = l + [i] -            dfs(tmpl) -dfs([]) +l = itertools.combinations_with_replacement(list(range(1, m + 1)), n) +ans = 0 +for li in l: +    ans = max(check(li), ans)