def comb(n,r):

    r=min(r,n-r)

    c=1

    for i in range(r):

        c=c*(n-i)/(r-i)

    return int(c)



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

a=[list(map(int,input().split())) for _ in range(Q)]



npat=comb(N+M,M)

A=[1 for _ in range(N)]

ans=0



for i in range(npat):

    temp=0

    for j in range(Q):

        if A[a[j][1]-1]-A[a[j][0]-1]==a[j][2]:

            temp+=a[j][3]

    ans=max(ans,temp)

#    print(A)

#    print(temp)

    A[-1]+=1

    if A[-1]>M and i<npat-1:

        k=1

        while max(A)>M and k<N:

            A[N-k-1]+=1

            A[N-k]=A[N-k-1]

            k+=1

            for l in range(1,k):

                A[N-l]=A[N-k]

    if min(A)>M:

        break

print(ans)
def dfs(A):

    if len(A)==N:

        temp=0

        for j in range(Q):

            if A[a[j][1]-1]-A[a[j][0]-1]==a[j][2]:

                temp+=a[j][3]

        ans.append(temp)

        return

    else:

        if len(A)>0:

            m=A[-1]

        else:

            m=1

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

            A.append(i)

            dfs(A)

            A.pop()

        

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

a=[list(map(int,input().split())) for _ in range(Q)]

ans=[]

dfs([])

print((max(ans)))
def comb(n, r):
    r = min(r, n - r)
    c = 1
    for i in range(r):
        c = c * (n - i) / (r - i)
    return int(c)


N, M, Q = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(Q)]
npat = comb(N + M, M)
A = [1 for _ in range(N)]
ans = 0
for i in range(npat):
    temp = 0
    for j in range(Q):
        if A[a[j][1] - 1] - A[a[j][0] - 1] == a[j][2]:
            temp += a[j][3]
    ans = max(ans, temp)
    #    print(A)
    #    print(temp)
    A[-1] += 1
    if A[-1] > M and i < npat - 1:
        k = 1
        while max(A) > M and k < N:
            A[N - k - 1] += 1
            A[N - k] = A[N - k - 1]
            k += 1
            for l in range(1, k):
                A[N - l] = A[N - k]
    if min(A) > M:
        break
print(ans)

def dfs(A):
    if len(A) == N:
        temp = 0
        for j in range(Q):
            if A[a[j][1] - 1] - A[a[j][0] - 1] == a[j][2]:
                temp += a[j][3]
        ans.append(temp)
        return
    else:
        if len(A) > 0:
            m = A[-1]
        else:
            m = 1
        for i in range(m, M + 1):
            A.append(i)
            dfs(A)
            A.pop()


N, M, Q = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(Q)]
ans = []
dfs([])
print((max(ans)))

-def comb(n, r): -    r = min(r, n - r) -    c = 1 -    for i in range(r): -        c = c * (n - i) / (r - i) -    return int(c) +def dfs(A): +    if len(A) == N: +        temp = 0 +        for j in range(Q): +            if A[a[j][1] - 1] - A[a[j][0] - 1] == a[j][2]: +                temp += a[j][3] +        ans.append(temp) +        return +    else: +        if len(A) > 0: +            m = A[-1] +        else: +            m = 1 +        for i in range(m, M + 1): +            A.append(i) +            dfs(A) +            A.pop() -npat = comb(N + M, M) -A = [1 for _ in range(N)] -ans = 0 -for i in range(npat): -    temp = 0 -    for j in range(Q): -        if A[a[j][1] - 1] - A[a[j][0] - 1] == a[j][2]: -            temp += a[j][3] -    ans = max(ans, temp) -    #    print(A) -    #    print(temp) -    A[-1] += 1 -    if A[-1] > M and i < npat - 1: -        k = 1 -        while max(A) > M and k < N: -            A[N - k - 1] += 1 -            A[N - k] = A[N - k - 1] -            k += 1 -            for l in range(1, k): -                A[N - l] = A[N - k] -    if min(A) > M: -        break -print(ans) +ans = [] +dfs([]) +print((max(ans)))