n,m,q = map(int,input().split())
ABCD = [list(map(int,input().split())) for i in range(q)]

ANS = []

if n == 2:
    for a in range(1,m+1):
        for b in range(a,m+1):
            ANS.append([a,b])
            
if n == 3:
    for a in range(1,m+1):
        for b in range(a,m+1):
            for c in range(b,m+1):
                ANS.append([a,b,c])
                
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):
                    ANS.append([a,b,c,d])
                    
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):
                        ANS.append([a,b,c,d,e])
                        
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):
                            ANS.append([a,b,c,d,e,f])
                        
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):
                                ANS.append([a,b,c,d,e,f,g])
                                
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):
                                    ANS.append([a,b,c,d,e,f,g,h])
                                    
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):
                                        ANS.append([a,b,c,d,e,f,g,h,i])
                                        
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):
                                            ANS.append([a,b,c,d,e,f,g,h,i,j])
                                            
ans = 0

for i in ANS:
    point = 0
    for j in ABCD:
        a = j[0] - 1
        b = j[1] - 1
        c = j[2]
        d = j[3]
        
        if i[b] - i[a] == c:
            point += d
            
    ans = max(ans,point)
    
print(ans)

Measured memory_bytes: 86392