import sys

sys.setrecursionlimit(10 ** 9)

input = sys.stdin.readline



from itertools import permutations,  combinations, accumulate, product, combinations_with_replacement

from functools import *

from collections import deque, defaultdict, Counter

from heapq import heapify, heappop, heappush, heappushpop





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

abcd_ls = []



for _ in range(Q):

    abcd_ls.append(list(map(int, input().split())))



a_candidate = list(combinations_with_replacement(list(range(1, M + 1)), N))



max_total = 0

for A in a_candidate:

    total = 0

    for abcd in abcd_ls:

        a, b, c, d = abcd

        a -= 1

        b -= 1

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

            total += d

    max_total = max(total, max_total)



print(max_total)



import sys

import itertools



sys.setrecursionlimit(10 ** 9)

input = sys.stdin.readline





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

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





def calc():

    score = 0

    for abcd in abcd_ls:

        a, b, c, d = abcd

        a -= 1

        b -= 1

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

            score += d

    return score





def dfs(i, mx):

    if i == N:

        return calc()



    score = 0

    for x in range(mx, M+1):

        A[i] = x

        s = dfs(i+1, x)

        score = max(s, score)

    return score





A = [0] * N

ans = dfs(0, 1)

print(ans)

import sys

sys.setrecursionlimit(10**9)
input = sys.stdin.readline
from itertools import (
    permutations,
    combinations,
    accumulate,
    product,
    combinations_with_replacement,
)
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

N, M, Q = list(map(int, input().split()))
abcd_ls = []
for _ in range(Q):
    abcd_ls.append(list(map(int, input().split())))
a_candidate = list(combinations_with_replacement(list(range(1, M + 1)), N))
max_total = 0
for A in a_candidate:
    total = 0
    for abcd in abcd_ls:
        a, b, c, d = abcd
        a -= 1
        b -= 1
        if c == A[b] - A[a]:
            total += d
    max_total = max(total, max_total)
print(max_total)

import sys
import itertools

sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N, M, Q = list(map(int, input().split()))
abcd_ls = [list(map(int, input().split())) for _ in range(Q)]


def calc():
    score = 0
    for abcd in abcd_ls:
        a, b, c, d = abcd
        a -= 1
        b -= 1
        if c == A[b] - A[a]:
            score += d
    return score


def dfs(i, mx):
    if i == N:
        return calc()
    score = 0
    for x in range(mx, M + 1):
        A[i] = x
        s = dfs(i + 1, x)
        score = max(s, score)
    return score


A = [0] * N
ans = dfs(0, 1)
print(ans)

+import itertools -from itertools import ( -    permutations, -    combinations, -    accumulate, -    product, -    combinations_with_replacement, -) -from functools import * -from collections import deque, defaultdict, Counter -from heapq import heapify, heappop, heappush, heappushpop +N, M, Q = list(map(int, input().split())) +abcd_ls = [list(map(int, input().split())) for _ in range(Q)] -N, M, Q = list(map(int, input().split())) -abcd_ls = [] -for _ in range(Q): -    abcd_ls.append(list(map(int, input().split()))) -a_candidate = list(combinations_with_replacement(list(range(1, M + 1)), N)) -max_total = 0 -for A in a_candidate: -    total = 0 + +def calc(): +    score = 0 -            total += d -    max_total = max(total, max_total) -print(max_total) +            score += d +    return score + + +def dfs(i, mx): +    if i == N: +        return calc() +    score = 0 +    for x in range(mx, M + 1): +        A[i] = x +        s = dfs(i + 1, x) +        score = max(s, score) +    return score + + +A = [0] * N +ans = dfs(0, 1) +print(ans)