from pprint import pprint

 

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

 

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

 

#print(n)

#print(m)

#print(q)

#pprint(ls)

 

masterlist = []

minilist = [0] * n

i = 0

 

 

def saiki(before_a,i,minilist):

    if i > n-1:

        masterlist.append([minilist[j] for j in range(n)])

        return

    for Ai in range(before_a,m+1):

        minilist[i] = Ai

        saiki(Ai,i+1,minilist)

 

#for before_a in range(1,m+1):

#    saiki(before_a,where,minilist)

 

before_a = 1

saiki(before_a,i,minilist)

 

#pprint(masterlist)

#pprint(ql)

 

dmax = 0

 

for a_mini in masterlist:

    dtemp = 0

    for ql_mini in ql:

        if a_mini[ql_mini[1]-1] - a_mini[ql_mini[0]-1] == ql_mini[2]:

            dtemp += ql_mini[3]

        else:

            pass

    if dtemp >= dmax:

        dmax = dtemp

 

print(dmax)
from pprint import pprint

import itertools





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



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







a_lists = list(itertools.combinations_with_replacement(list(range(m)), n))



#print(a_lists)



dmax = 0



for a_mini in a_lists:

    dtemp = 0

    for ql_mini in ql:

        if a_mini[ql_mini[1]-1] - a_mini[ql_mini[0]-1] == ql_mini[2]:

            dtemp += ql_mini[3]

        else:

            pass

    if dtemp >= dmax:

        dmax = dtemp



print(dmax)
from pprint import pprint

n, m, q = list(map(int, input().split()))
ql = [list(map(int, input().split())) for _ in range(q)]
# print(n)
# print(m)
# print(q)
# pprint(ls)
masterlist = []
minilist = [0] * n
i = 0


def saiki(before_a, i, minilist):
    if i > n - 1:
        masterlist.append([minilist[j] for j in range(n)])
        return
    for Ai in range(before_a, m + 1):
        minilist[i] = Ai
        saiki(Ai, i + 1, minilist)


# for before_a in range(1,m+1):
#    saiki(before_a,where,minilist)
before_a = 1
saiki(before_a, i, minilist)
# pprint(masterlist)
# pprint(ql)
dmax = 0
for a_mini in masterlist:
    dtemp = 0
    for ql_mini in ql:
        if a_mini[ql_mini[1] - 1] - a_mini[ql_mini[0] - 1] == ql_mini[2]:
            dtemp += ql_mini[3]
        else:
            pass
    if dtemp >= dmax:
        dmax = dtemp
print(dmax)

from pprint import pprint
import itertools

n, m, q = list(map(int, input().split()))
ql = [list(map(int, input().split())) for _ in range(q)]
a_lists = list(itertools.combinations_with_replacement(list(range(m)), n))
# print(a_lists)
dmax = 0
for a_mini in a_lists:
    dtemp = 0
    for ql_mini in ql:
        if a_mini[ql_mini[1] - 1] - a_mini[ql_mini[0] - 1] == ql_mini[2]:
            dtemp += ql_mini[3]
        else:
            pass
    if dtemp >= dmax:
        dmax = dtemp
print(dmax)

+import itertools -# print(n) -# print(m) -# print(q) -# pprint(ls) -masterlist = [] -minilist = [0] * n -i = 0 - - -def saiki(before_a, i, minilist): -    if i > n - 1: -        masterlist.append([minilist[j] for j in range(n)]) -        return -    for Ai in range(before_a, m + 1): -        minilist[i] = Ai -        saiki(Ai, i + 1, minilist) - - -# for before_a in range(1,m+1): -#    saiki(before_a,where,minilist) -before_a = 1 -saiki(before_a, i, minilist) -# pprint(masterlist) -# pprint(ql) +a_lists = list(itertools.combinations_with_replacement(list(range(m)), n)) +# print(a_lists) -for a_mini in masterlist: +for a_mini in a_lists: