#実験

# -*- coding: utf-8 -*-

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

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

a = [1]

score = 0

 

# 深さ優先探索(Depth First Search)

def dfs(seq):

    if len(seq) == n:

        global score

        tmp_score = 0

        for a, b, c, d in integer_set:

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

                tmp_score += d

            else:

                tmp_score += 0

        score = max(score, tmp_score)

    else:

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

            tmp_seq = seq.copy()

            tmp_seq.append(i)

            dfs(tmp_seq)

 

dfs(a)

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

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

S=[1]*N#1がNこ入った配列　

 

def dfs(d):

    if d==N-1:

        t=0

        for a,b,c,d in R:

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

                t+=d

        return t

    else:

        m=0

        for i in range(S[d],M+1):

            S[d+1]=i

            m=max(m,dfs(d+1))

        return m



print((dfs(0)))
# 実験
# -*- coding: utf-8 -*-
n, m, q = list(map(int, input().split()))
integer_set = [list(map(int, input().split())) for _ in range(q)]
a = [1]
score = 0
# 深さ優先探索(Depth First Search)
def dfs(seq):
    if len(seq) == n:
        global score
        tmp_score = 0
        for a, b, c, d in integer_set:
            if seq[b - 1] - seq[a - 1] == c:
                tmp_score += d
            else:
                tmp_score += 0
        score = max(score, tmp_score)
    else:
        for i in range(seq[-1], m + 1):
            tmp_seq = seq.copy()
            tmp_seq.append(i)
            dfs(tmp_seq)


dfs(a)
print(score)

N, M, Q = list(map(int, input().split()))
R = [list(map(int, input().split())) for i in range(Q)]
S = [1] * N  # 1がNこ入った配列


def dfs(d):
    if d == N - 1:
        t = 0
        for a, b, c, d in R:
            if S[b - 1] - S[a - 1] == c:
                t += d
        return t
    else:
        m = 0
        for i in range(S[d], M + 1):
            S[d + 1] = i
            m = max(m, dfs(d + 1))
        return m


print((dfs(0)))

-# 実験 -# -*- coding: utf-8 -*- -n, m, q = list(map(int, input().split())) -integer_set = [list(map(int, input().split())) for _ in range(q)] -a = [1] -score = 0 -# 深さ優先探索(Depth First Search) -def dfs(seq): -    if len(seq) == n: -        global score -        tmp_score = 0 -        for a, b, c, d in integer_set: -            if seq[b - 1] - seq[a - 1] == c: -                tmp_score += d -            else: -                tmp_score += 0 -        score = max(score, tmp_score) -    else: -        for i in range(seq[-1], m + 1): -            tmp_seq = seq.copy() -            tmp_seq.append(i) -            dfs(tmp_seq) +N, M, Q = list(map(int, input().split())) +R = [list(map(int, input().split())) for i in range(Q)] +S = [1] * N  # 1がNこ入った配列 -dfs(a) -print(score) +def dfs(d): +    if d == N - 1: +        t = 0 +        for a, b, c, d in R: +            if S[b - 1] - S[a - 1] == c: +                t += d +        return t +    else: +        m = 0 +        for i in range(S[d], M + 1): +            S[d + 1] = i +            m = max(m, dfs(d + 1)) +        return m + + +print((dfs(0)))