def dfs(seq):

    ans = 0

    if len(seq) == n:

        kou = 0

        for u in data:

            if seq[u[1]-1] - seq[u[0]-1] == u[2]:

                kou+=u[3]

        return kou

    else:#len(seq)==N-1からの遷都を考えると良い

        for i in range(seq[-1], m+1):

            seq_next=seq+[i]

            ans = max(ans, dfs(seq_next))

        return ans



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

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



score = dfs([1])

print(score)

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

def dfs(seq):

    u=0

    if len(seq)==n:

        ans=0

        for a,b,c,d in data:

            if seq[b-1]-seq[a-1]==c:

                ans+=d

        return ans

    else:

        for i in range(seq[-1],m+1):

            seq_next=seq+[i]

            u=max(u,dfs(seq_next))

        return u



data=[]

for _ in range(q):

    A=list(map(int,input().split()))

    data.append(A)

print((dfs([1])))

def dfs(seq):
    ans = 0
    if len(seq) == n:
        kou = 0
        for u in data:
            if seq[u[1] - 1] - seq[u[0] - 1] == u[2]:
                kou += u[3]
        return kou
    else:  # len(seq)==N-1からの遷都を考えると良い
        for i in range(seq[-1], m + 1):
            seq_next = seq + [i]
            ans = max(ans, dfs(seq_next))
        return ans


n, m, q = list(map(int, input().split()))
data = [list(map(int, input().split())) for _ in range(q)]
score = dfs([1])
print(score)

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


def dfs(seq):
    u = 0
    if len(seq) == n:
        ans = 0
        for a, b, c, d in data:
            if seq[b - 1] - seq[a - 1] == c:
                ans += d
        return ans
    else:
        for i in range(seq[-1], m + 1):
            seq_next = seq + [i]
            u = max(u, dfs(seq_next))
        return u


data = []
for _ in range(q):
    A = list(map(int, input().split()))
    data.append(A)
print((dfs([1])))

+n, m, q = list(map(int, input().split())) + + -    ans = 0 +    u = 0 -        kou = 0 -        for u in data: -            if seq[u[1] - 1] - seq[u[0] - 1] == u[2]: -                kou += u[3] -        return kou -    else:  # len(seq)==N-1からの遷都を考えると良い +        ans = 0 +        for a, b, c, d in data: +            if seq[b - 1] - seq[a - 1] == c: +                ans += d +        return ans +    else: -            ans = max(ans, dfs(seq_next)) -        return ans +            u = max(u, dfs(seq_next)) +        return u -n, m, q = list(map(int, input().split())) -data = [list(map(int, input().split())) for _ in range(q)] -score = dfs([1]) -print(score) +data = [] +for _ in range(q): +    A = list(map(int, input().split())) +    data.append(A) +print((dfs([1])))