NMQ = list(map(int, input().split()))
N = NMQ[0]
M = NMQ[1]
Q = NMQ[2]
listOfLists = []
Qlist = []
for i in range(Q):
    Qlist.append(list(map(int, input().split())))
if N == 2:
    for a in range(1,M+1):
        for b in range(a,M+1):
            listOfLists.append([1,a,b,M])
if N == 3:
    for a in range(1,M+1):
        for b in range(a,M+1):
            for c in range(b,M+1):
                listOfLists.append([1,a,b,c,M])
if N == 4:
    for a in range(1,M+1):
        for b in range(a,M+1):
            for c in range(b,M+1):
                for d in range(c,M+1):
                    listOfLists.append([1,a,b,c,d,M])
if N == 5:
    for a in range(1,M+1):
        for b in range(a,M+1):
            for c in range(b,M+1):
                for d in range(c,M+1):
                    for e in range(d,M+1):
                        listOfLists.append([1,a,b,c,d,e,M])
if N == 6:
    for a in range(1,M+1):
        for b in range(a,M+1):
            for c in range(b,M+1):
                for d in range(c,M+1):
                    for e in range(d,M+1):
                        for f in range(e,M+1):
                            listOfLists.append([1,a,b,c,d,e,f,M])
if N == 7:
    for a in range(1,M+1):
        for b in range(a,M+1):
            for c in range(b,M+1):
                for d in range(c,M+1):
                    for e in range(d,M+1):
                        for f in range(e,M+1):
                            for g in range(f,M+1):
                                listOfLists.append([1,a,b,c,d,e,f,g,M])
if N == 8:
    for a in range(1,M+1):
        for b in range(a,M+1):
            for c in range(b,M+1):
                for d in range(c,M+1):
                    for e in range(d,M+1):
                        for f in range(e,M+1):
                            for g in range(f,M+1):
                                for h in range(g,M+1):
                                    listOfLists.append([1,a,b,c,d,e,f,g,h,M])
if N == 9:
    for a in range(1,M+1):
        for b in range(a,M+1):
            for c in range(b,M+1):
                for d in range(c,M+1):
                    for e in range(d,M+1):
                        for f in range(e,M+1):
                            for g in range(f,M+1):
                                for h in range(g,M+1):
                                    for i in range(h,M+1):
                                        listOfLists.append([1,a,b,c,d,e,f,g,h,i,M])
if N == 10:
    for a in range(1,M+1):
        for b in range(a,M+1):
            for c in range(b,M+1):
                for d in range(c,M+1):
                    for e in range(d,M+1):
                        for f in range(e,M+1):
                            for g in range(f,M+1):
                                for h in range(g,M+1):
                                    for i in range(h,M+1):
                                        for j in range(i,M+1):
                                            listOfLists.append([1,a,b,c,d,e,f,g,h,i,j,M])
scoreList = []
def cal_score(a,b):
    score = 0
    if a[b[1]] - a[b[0]] == b[2]:
        score = b[3]
    return score

for item in listOfLists:
    setResult = []
    for j in Qlist:
        setResult.append(cal_score(item,j))
    scoreList.append(sum(setResult))
print(max(scoreList))

Measured memory_bytes: 28152