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

a,b=[],[]

c,d=[],[]

for i in range(q):

  aa,bb,cc,dd=list(map(int,input().split()))

  a.append(aa-1)

  b.append(bb-1)

  c.append(cc)

  d.append(dd)

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

from itertools import combinations_with_replacement

ans=0

for ll in list(combinations_with_replacement(l,n)):

  t=0

  for i in range(q):

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

      t+=d[i]

  ans=max(ans,t)

print(ans)
def calc(l):

  t=0

  for ai,bi,ci,di in zip(a,b,c,d):

    if l[bi]-l[ai]==ci:

      t+=di

  return t



def dfs(l):

  if len(l)==n:

    return calc(l)

  res=0

  prev_last=0

  if len(l)>0:

    prev_last=l[-1]

  for v in range(prev_last,m):

    l.append(v)

    res=max(res,dfs(l))

    l.pop()

  return res



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

a,b=[],[]

c,d=[],[]

for i in range(q):

  aa,bb,cc,dd=list(map(int,input().split()))

  a.append(aa-1)

  b.append(bb-1)

  c.append(cc)

  d.append(dd)



print((dfs([])))





n, m, q = list(map(int, input().split()))
a, b = [], []
c, d = [], []
for i in range(q):
    aa, bb, cc, dd = list(map(int, input().split()))
    a.append(aa - 1)
    b.append(bb - 1)
    c.append(cc)
    d.append(dd)
l = [int(i) for i in range(1, m + 1)]
from itertools import combinations_with_replacement

ans = 0
for ll in list(combinations_with_replacement(l, n)):
    t = 0
    for i in range(q):
        if ll[b[i]] - ll[a[i]] == c[i]:
            t += d[i]
    ans = max(ans, t)
print(ans)

def calc(l):
    t = 0
    for ai, bi, ci, di in zip(a, b, c, d):
        if l[bi] - l[ai] == ci:
            t += di
    return t


def dfs(l):
    if len(l) == n:
        return calc(l)
    res = 0
    prev_last = 0
    if len(l) > 0:
        prev_last = l[-1]
    for v in range(prev_last, m):
        l.append(v)
        res = max(res, dfs(l))
        l.pop()
    return res


n, m, q = list(map(int, input().split()))
a, b = [], []
c, d = [], []
for i in range(q):
    aa, bb, cc, dd = list(map(int, input().split()))
    a.append(aa - 1)
    b.append(bb - 1)
    c.append(cc)
    d.append(dd)
print((dfs([])))

+def calc(l): +    t = 0 +    for ai, bi, ci, di in zip(a, b, c, d): +        if l[bi] - l[ai] == ci: +            t += di +    return t + + +def dfs(l): +    if len(l) == n: +        return calc(l) +    res = 0 +    prev_last = 0 +    if len(l) > 0: +        prev_last = l[-1] +    for v in range(prev_last, m): +        l.append(v) +        res = max(res, dfs(l)) +        l.pop() +    return res + + -l = [int(i) for i in range(1, m + 1)] -from itertools import combinations_with_replacement - -ans = 0 -for ll in list(combinations_with_replacement(l, n)): -    t = 0 -    for i in range(q): -        if ll[b[i]] - ll[a[i]] == c[i]: -            t += d[i] -    ans = max(ans, t) -print(ans) +print((dfs([])))