def solve():

    N,M,Q = list(map(int,input().split()))

    req = []

    for i in range(Q):

        a,b,c,d = list(map(int,input().split()))

        a -= 1

        b -= 1

        req.append([a,b,c,d])



    res = []

    dfs(list(), res,M,N)



    mx = 0

    for p in res:

        point = 0

        for a,b,c,d in req:

            if p[b] - p[a] == c:

                point += d

        mx = max(point, mx)

    

    print(mx)





def dfs(now,res,M,N):

    if len(now) == N:

        res.append(now)

        return

    start = 1

    if len(now) != 0:

        start = now[-1]



    if start > M:

        return



    for i in range(start,M+1):

        nxt = now[:]

        nxt.append(i)

        dfs(nxt, res,M,N)

    

if __name__ == '__main__':

    solve()
def solve():

    N,M,Q = list(map(int,input().split()))

    global info

    info = []

    for _ in range(Q):

        a,b,c,d = list(map(int,input().split()))

        a -= 1

        b -= 1

        info.append([a,b,c,d])



    print((dfs(list(),M,N)))



def dfs(A,M,N):

    if len(A) == N:

        return calcPoint(A)

    

    res = 0

    prev = A[-1] if len(A) > 0 else 1

    for i in range(prev,M+1):

        A.append(i)

        res = max(res,dfs(A,M,N))

        A.pop()

    

    return res



def calcPoint(A):

    result = 0

    for a,b,c,d in info:

        if A[b] - A[a] == c:

            result += d

    return result

                

if __name__ == '__main__':

    solve()
def solve():
    N, M, Q = list(map(int, input().split()))
    req = []
    for i in range(Q):
        a, b, c, d = list(map(int, input().split()))
        a -= 1
        b -= 1
        req.append([a, b, c, d])
    res = []
    dfs(list(), res, M, N)
    mx = 0
    for p in res:
        point = 0
        for a, b, c, d in req:
            if p[b] - p[a] == c:
                point += d
        mx = max(point, mx)
    print(mx)


def dfs(now, res, M, N):
    if len(now) == N:
        res.append(now)
        return
    start = 1
    if len(now) != 0:
        start = now[-1]
    if start > M:
        return
    for i in range(start, M + 1):
        nxt = now[:]
        nxt.append(i)
        dfs(nxt, res, M, N)


if __name__ == "__main__":
    solve()

def solve():
    N, M, Q = list(map(int, input().split()))
    global info
    info = []
    for _ in range(Q):
        a, b, c, d = list(map(int, input().split()))
        a -= 1
        b -= 1
        info.append([a, b, c, d])
    print((dfs(list(), M, N)))


def dfs(A, M, N):
    if len(A) == N:
        return calcPoint(A)
    res = 0
    prev = A[-1] if len(A) > 0 else 1
    for i in range(prev, M + 1):
        A.append(i)
        res = max(res, dfs(A, M, N))
        A.pop()
    return res


def calcPoint(A):
    result = 0
    for a, b, c, d in info:
        if A[b] - A[a] == c:
            result += d
    return result


if __name__ == "__main__":
    solve()

-    req = [] -    for i in range(Q): +    global info +    info = [] +    for _ in range(Q): -        req.append([a, b, c, d]) -    res = [] -    dfs(list(), res, M, N) -    mx = 0 -    for p in res: -        point = 0 -        for a, b, c, d in req: -            if p[b] - p[a] == c: -                point += d -        mx = max(point, mx) -    print(mx) +        info.append([a, b, c, d]) +    print((dfs(list(), M, N))) -def dfs(now, res, M, N): -    if len(now) == N: -        res.append(now) -        return -    start = 1 -    if len(now) != 0: -        start = now[-1] -    if start > M: -        return -    for i in range(start, M + 1): -        nxt = now[:] -        nxt.append(i) -        dfs(nxt, res, M, N) +def dfs(A, M, N): +    if len(A) == N: +        return calcPoint(A) +    res = 0 +    prev = A[-1] if len(A) > 0 else 1 +    for i in range(prev, M + 1): +        A.append(i) +        res = max(res, dfs(A, M, N)) +        A.pop() +    return res + + +def calcPoint(A): +    result = 0 +    for a, b, c, d in info: +        if A[b] - A[a] == c: +            result += d +    return result