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

#N: 正整数列の長さ, M:正整数列の最大値, Q: 4つの整数の組みの数



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



def dfs(now, n, num): #nowはこれから生成するもの

    if n == 0:

        make_group.append(now)

    else:

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

            dfs(now+[i], n-1, i)



make_group = []

dfs([], N, 1)

ans = 0

for group in make_group:

    tmp = 0

    for q in Q_li:

        a = q[0] - 1

        b = q[1] - 1

        if q[2] == (group[b] - group[a]):

            tmp += q[3]

    ans = max(ans, tmp)

print(ans)



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

#N: 正整数列の長さ, M:正整数列の最大値, Q: 4つの整数の組みの数



a_l = [0]*Q

b_l = [0]*Q

c_l = [0]*Q

d_l = [0]*Q

for i in range(Q):

    a_l[i], b_l[i], c_l[i], d_l[i] = list(map(int, input().split()))



def calc_score(now):

    score = 0

    for a, b, c, d in zip(a_l, b_l, c_l, d_l):

        a -= 1

        b -= 1

        if c == (now[b]-now[a]):

            score += d



    return score



def dfs(now, n, num): #nowはこれから生成するもの

    if n == 0:

        score_list.append(calc_score(now))

    else:

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

            dfs(now+[i], n-1, i)



score_list = []

dfs([], N, 1)

print((max(score_list)))

N, M, Q = list(map(int, input().split()))
# N: 正整数列の長さ, M:正整数列の最大値, Q: 4つの整数の組みの数
Q_li = [list(map(int, input().split())) for _ in range(Q)]


def dfs(now, n, num):  # nowはこれから生成するもの
    if n == 0:
        make_group.append(now)
    else:
        for i in range(num, M + 1):
            dfs(now + [i], n - 1, i)


make_group = []
dfs([], N, 1)
ans = 0
for group in make_group:
    tmp = 0
    for q in Q_li:
        a = q[0] - 1
        b = q[1] - 1
        if q[2] == (group[b] - group[a]):
            tmp += q[3]
    ans = max(ans, tmp)
print(ans)

N, M, Q = list(map(int, input().split()))
# N: 正整数列の長さ, M:正整数列の最大値, Q: 4つの整数の組みの数
a_l = [0] * Q
b_l = [0] * Q
c_l = [0] * Q
d_l = [0] * Q
for i in range(Q):
    a_l[i], b_l[i], c_l[i], d_l[i] = list(map(int, input().split()))


def calc_score(now):
    score = 0
    for a, b, c, d in zip(a_l, b_l, c_l, d_l):
        a -= 1
        b -= 1
        if c == (now[b] - now[a]):
            score += d
    return score


def dfs(now, n, num):  # nowはこれから生成するもの
    if n == 0:
        score_list.append(calc_score(now))
    else:
        for i in range(num, M + 1):
            dfs(now + [i], n - 1, i)


score_list = []
dfs([], N, 1)
print((max(score_list)))

-Q_li = [list(map(int, input().split())) for _ in range(Q)] +a_l = [0] * Q +b_l = [0] * Q +c_l = [0] * Q +d_l = [0] * Q +for i in range(Q): +    a_l[i], b_l[i], c_l[i], d_l[i] = list(map(int, input().split())) + + +def calc_score(now): +    score = 0 +    for a, b, c, d in zip(a_l, b_l, c_l, d_l): +        a -= 1 +        b -= 1 +        if c == (now[b] - now[a]): +            score += d +    return score -        make_group.append(now) +        score_list.append(calc_score(now)) -make_group = [] +score_list = [] -ans = 0 -for group in make_group: -    tmp = 0 -    for q in Q_li: -        a = q[0] - 1 -        b = q[1] - 1 -        if q[2] == (group[b] - group[a]): -            tmp += q[3] -    ans = max(ans, tmp) -print(ans) +print((max(score_list)))