Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/python3
import math
import copy

def minx(idx, cl, m):
    mi = 1
    for i in range(idx):
        mi = max(mi, cl[i])
    return mi

def maxx(idx, cl, m):
    ma = m
    for i in range(idx + 1, len(cl)):
        if cl[i] > 0 and cl[i] < ma:
            ma = cl[i]
    return ma

def maxp(qs, idx, cl, n, m, cache):
    k = str(idx) + ':' + ':'.join(map(str, cl))
    if k in cache:
        return cache[k]

    (a, b, c, d) = qs[idx]

    if idx == len(qs) - 1:
        mp = 0
        if cl[a - 1] == 0 and cl[b - 1] == 0:
            mia = minx(a - 1, cl, m)
            maa = maxx(a - 1, cl, m)

            mib = minx(b - 1, cl, m)
            mab = maxx(b - 1, cl, m)
            
            for i in range(mia, maa + 1):
                if mib <= i + c <= mab:
                    mp = d
                    break
                    
        elif cl[a - 1] == 0:
            na = cl[b - 1] - c
            if na > 0:
                mia = minx(a - 1, cl, m)
                maa = maxx(a - 1, cl, m)
                if mia <= na <= maa:
                    mp = d
                    
        elif cl[b - 1] == 0:
            nb = cl[a - 1] + c
            if nb <= m:
                mib = minx(b - 1, cl, m)
                mab = maxx(b - 1, cl, m)                

                if mib <= nb <= mab:
                    mp = d

        elif cl[b - 1] - cl[a - 1] == c:
            mp = d

        cache[k] = mp
    else:
        #not select
        mp = maxp(qs, idx + 1, cl, n, m, cache)

        if cl[a - 1] == 0 and cl[b - 1] == 0:
            mia = minx(a - 1, cl, m)
            maa = maxx(a - 1, cl, m)

            mib = minx(b - 1, cl, m)
            mab = maxx(b - 1, cl, m)
            
            for i in range(mia, maa + 1):
                if mib <= i + c <= mab:
                    ncl = copy.copy(cl)
                    ncl[a - 1] = i
                    ncl[b - 1] = i + c
                    mp = max(mp, d + maxp(qs, idx + 1, ncl, n, m, cache))
                    
        elif cl[a - 1] == 0:
            na = cl[b - 1] - c
            if na > 0:
                mia = minx(a - 1, cl, m)
                maa = maxx(a - 1, cl, m)                
                if mia <= na <= maa:
                    ncl = copy.copy(cl)
                    ncl[a - 1] = na
                    mp = max(mp, d + maxp(qs, idx + 1, ncl, n, m, cache))
                    
        elif cl[b - 1] == 0:
            nb = cl[a - 1] + c
            if nb <= m:
                mib = minx(b - 1, cl, m)
                mab = maxx(b - 1, cl, m)                
                if mib <= nb <= mab:
                    ncl = copy.copy(cl)                    
                    ncl[b - 1] = nb
                    mp = max(mp, d + maxp(qs, idx + 1, ncl, n, m, cache))
                    
        else:
            if cl[b - 1] - cl[a - 1] == c:
                mp = max(mp, d + maxp(qs, idx + 1, cl, n, m, cache))
                
        cache[k] = mp

    return cache[k]

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

qs = []
for i in range(q):
    qs.append(tuple(map(int, input().split())))

qs.sort()
         
l = [ 0 for i in range(n) ]
l[0] = 1
cache = {}

print(maxp(qs, 0, l, n, m, cache))
    





Fixed submission:
#!/usr/bin/python3
import math
import copy

def getp(qs, cl):
    p = 0
    for (a, b, c, d) in qs:
        if cl [b - 1] - cl[a - 1] == c:
            p += d
    return p

def maxp(qs, idx, cl, n, m):
    p = 0    
    if idx == n - 1:
        for i in range(cl[idx - 1], m + 1):
            cl[idx] = i
            p = max(p, getp(qs, cl))
    else:
        for i in range(cl[idx - 1], m + 1):
            cl[idx] = i
            p = max(p, maxp(qs, idx + 1, cl, n, m))
    return p        
    
(n, m, q) = map(int, input().split())

qs = []
for i in range(q):
    qs.append(tuple(map(int, input().split())))

qs.sort()
         
l = [ 0 for i in range(n) ]
l[0] = 1
cache = {}

print(maxp(qs, 1, l, n, m))