n, m, q = map(int, input().split())
abcd = []
for _ in range(q):
    abcd_ = list(map(int, input().split()))
    abcd_[0] -= 1
    abcd_[1] -= 1
    abcd.append(abcd_)

ans =0
    
if n == 2:
    for a in range(1, m+1):
        for b in range(a, m+1):
            A = [a, b]
            ans_ = 0
            for res in abcd:
                if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
            ans = max(ans, ans_)   
            
elif n == 3:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                A = [a, b, c]
                ans_ = 0
                for res in abcd:
                    if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                ans = max(ans, ans_)      
                
elif 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):
                    A = [a, b, c, d]
                    ans_ = 0
                    for res in abcd:
                        if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                    ans = max(ans, ans_)
                    
elif 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):
                        A = [a, b, c, d, e]
                        ans_ = 0
                        for res in abcd:
                            if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                        ans = max(ans, ans_)
                        
elif 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):
                            A = [a, b, c, d, e, f]
                            ans_ = 0
                            for res in abcd:
                                if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                            ans = max(ans, ans_)
                            
elif 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):
                                A = [a, b, c, d, e, f, g]
                                ans_ = 0
                                for res in abcd:
                                    if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                                ans = max(ans, ans_)
                                
elif 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):
                                    A = [a, b, c, d, e, f, g, h]
                                    ans_ = 0
                                    for res in abcd:
                                        if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                                    ans = max(ans, ans_)
                                    
elif 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):
                                        A = [a, b, c, d, e, f, g, h, i]
                                        ans_ = 0
                                        for res in abcd:
                                            if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                                        ans = max(ans, ans_)
                
elif 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):
                                            A = [a, b, c, d, e, f, g, h, i, j]
                                            ans_ = 0
                                            for res in abcd:
                                                if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                                            ans = max(ans, ans_)
                                                    
print(ans)

Measured memory_bytes: 72868