CodeEff/ECCO huggingface dataset:
# Original solution
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from operator import mul


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m, q = LI()
L = LIR(q)
ans = 0
def f(arr):
    global ans
    if len(arr) == n:
        ret = 0
        for a, b, c, d in L:
            if arr[b - 1] - arr[a - 1] == c:
                ret += d
        ans = max(ans, ret)
    else:
        if arr:
            for k in range(arr[-1], m + 1):
                f(arr + [k])
        else:
            for k in range(1, m + 1):
                f(arr + [k])

f([])
print(ans)

# Optimized solution
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from itertools import combinations_with_replacement, combinations
from operator import mul
from functools import reduce
from operator import mul


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m, q = LI()
L = LIR(q)
ans = 0
for arr in combinations_with_replacement(list(range(1, m + 1)), n):
    ret = 0
    for a, b, c, d in L:
        if arr[b - 1] - arr[a - 1] == c:
            ret += d
    ans = max(ans, ret)

print(ans)
# Original solution
N,M,Q=list(map(int,input().split()))
L = []
for i in range(Q):
  L.append(list(map(int, input().split())))
  L[i][0]-=1
  L[i][1]-=1

import itertools

l =list(range(1,M+1))
ans=0

v=list(itertools.combinations_with_replacement(l,N))

for arr in v:
  score=0
  for a,b,c,d in L:
    if (arr[b] - arr[a])==c:
      
      score+=d
  ans=max(ans,score)
print(ans)

# Optimized solution
'''ika tako
制約条件を見て、戦略を決める（制約が戦略を決める）
配列の添え字を、なるべくシンプルにする（KISS）
広義単調増加（同じ数もOK）なので、重複順列（仕切り板で分ける）
求める数列の候補である　重複順列を列挙し、重複順列毎にscoreを求め、
そのscoreの最大値が答（数列を求めるのではなく、数列を点検する）
入力は空のリストにappendで追加
入力の際、0-indexとなるように、引く1をする（後で考えるのはメンドウ）
重複順列毎に回し、更に、クエリ毎に条件式で得点を求める、二重ループ
scoreは、答の候補である重複順列毎の得点
求めるansは、そのscoreの最大値
この二つを区別すること
'''

from itertools import combinations_with_replacement
 
n, m, q = list(map(int, input().split()))
queries = []
for _ in range(q):
    a, b, c, d = list(map(int, input().split()))
    a -= 1
    b -= 1
    queries.append((a, b, c, d))

ans = 0
for arr in combinations_with_replacement(list(range(m)), n):
    score = 0
    for a, b, c, d in queries:
        if arr[b] - arr[a] == c:
            score += d
    ans = max(ans, score)

print(ans)
# Original solution
from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))
abcdn = [tuple(map(int, input().split())) for _ in range(Q)]

mx = 0
for AN in combinations_with_replacement(list(range(1, M+1)), N):
	score = 0
	for a, b, c, d in abcdn:
		score += d if AN[b-1] - AN[a-1] == c else 0
	mx = max(score, mx)
print(mx)

# Optimized solution
from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))
abcdn = [tuple(map(int, input().split())) for _ in range(Q)]

print((max([sum(d if AN[b-1] - AN[a-1] == c else 0 for a, b, c, d in abcdn) for AN in combinations_with_replacement(list(range(1, M+1)), N)])))
# Original solution
from itertools import combinations_with_replacement as c

N, M, Q = list(map(int, input().split()))
L = [tuple(map(int, input().split())) for _ in range(Q)]

print((max([sum(d if S[b-1] - S[a-1] == c else 0 for a, b, c, d in L) for S in c(list(range(1, M+1)), N)])))

# Optimized solution
from itertools import combinations_with_replacement as R

N,M,Q = list(map(int, input().split()))
L = [tuple(map(int, input().split())) for _ in range(Q)]

print((max([sum((d if S[b-1] - S[a-1] == c else 0) for a,b,c,d in L) for S in R(list(range(1, M+1)), N)])))
# Original solution
import itertools

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

answer = 0

nums=[]
for i in range(1, M+1):
    nums.append(i)

for balls in itertools.combinations_with_replacement(nums, N):
    score = 0
    for i in range(Q):
        test = sorted(list(balls))
        if test[abcd[i][0]-1] + abcd[i][2] == test[abcd[i][1]-1]:
            score += abcd[i][3]
    if score > answer:
        answer = score

print(answer)


# Optimized solution
import itertools

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

answer = 0

nums=[]
for i in range(1, M+1):
    nums.append(i)

for balls in itertools.combinations_with_replacement(nums, N):
    score = 0
    for i in range(Q):
        test = list(balls)
        if test[abcd[i][0]-1] + abcd[i][2] == test[abcd[i][1]-1]:
            score += abcd[i][3]
    if score > answer:
        answer = score

print(answer)
# Original solution
import itertools

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

answer = 0

nums=[]
for i in range(1, M+1):
    nums.append(i)

for balls in itertools.combinations_with_replacement(nums, N):
    score = 0
    for i in range(Q):
        test = list(balls)
        if test[abcd[i][0]-1] + abcd[i][2] == test[abcd[i][1]-1]:
            score += abcd[i][3]
    if score > answer:
        answer = score

print(answer)


# Optimized solution
import itertools

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

answer = 0

nums=[]
for i in range(1, M+1):
    nums.append(i)

for balls in itertools.combinations_with_replacement(nums, N):
    score = 0
    test = list(balls)
    for i in range(Q):
        if test[abcd[i][0]-1] + abcd[i][2] == test[abcd[i][1]-1]:
            score += abcd[i][3]
    if score > answer:
        answer = score

print(answer)
# Original solution
import copy
N, M, Q = list(map(int, input().split()))
A = []
B = []
C = []
D = []
ans = 0
def dfs(l):
    if len(l) == N+1:
        global ans
        now = 0
        for i in range(Q):
            if(l[B[i]] - l[A[i]] == C[i]):
                now += D[i]
        ans = max(ans, now)
    else:
        tmp = copy.copy(l)
        tmp.append(l[-1])
        while tmp[-1] <= M:
            dfs(tmp)
            tmp[-1] += 1

for i in range(Q):
    a, b, c, d = list(map(int, input().split()))
    A.append(a)
    B.append(b)
    C.append(c)
    D.append(d)

dfs([1])
print(ans)

# Optimized solution
import sys
sys.setrecursionlimit(10**9)
N, M, Q = list(map(int, input().split()))
A = []
B = []
C = []
D = []
ans = 0
def dfs(l):
    if len(l) == N+1:
        global ans
        now = 0
        for i in range(Q):
            if(l[B[i]] - l[A[i]] == C[i]):
                now += D[i]
        ans = max(ans, now)
    else:
        tmp = l[:]
        tmp.append(l[-1])
        while tmp[-1] <= M:
            dfs(tmp)
            tmp[-1] += 1

for i in range(Q):
    a, b, c, d = list(map(int, input().split()))
    A.append(a)
    B.append(b)
    C.append(c)
    D.append(d)

dfs([1])
print(ans)
# Original solution
#dfs試す
n,m,q=list(map(int, input().split( )))
qs=tuple(tuple(map(int, input().split( ))) for _ in range(q))
def dfs(ll):
    if len(ll)==n+1:
        sm=0
        for ai,bi,ci,di in qs:
            if ll[bi]-ll[ai]==ci:
                sm+=di 
        return sm
    res=[]
    for i in range(ll[-1],m):#+1
        ll2=ll+[i]
        res.append(dfs(ll2))
    return(max(res))
l=[0]
print((dfs(l)))


# Optimized solution
#dfs試す
n,m,q=list(map(int, input().split( )))
qs=tuple(tuple(map(int, input().split( ))) for _ in range(q))
ans=0
def dfs(ll):
    global ans
    if len(ll)==n+1:
        sm=0
        for ai,bi,ci,di in qs:
            if ll[bi]-ll[ai]==ci:
                sm+=di 
        ans=max(ans,sm)
    else:
        for i in range(ll[-1],m):#+1
            ll2=ll+[i]
            dfs(ll2)
l=[0]
dfs(l)
print(ans)
# Original solution
import itertools

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

Q_list = []
for _ in range(Q):
    q = list(map(int, input().split()))
    Q_list.append(q)

A_list = []
for a in itertools.combinations_with_replacement(list(range(1, M+1)), N):
    A_list.append(a)

answer = 0
score_list = []
for A in A_list:
    score = 0
    for i in range(Q):
        a, b, c, d = Q_list[i][0], Q_list[i][1], Q_list[i][2], Q_list[i][3]
        a -=1
        b -=1
        if A[b] - A[a] == c:
            score += d
    answer = max(answer, score)

print(answer)

# Optimized solution
import itertools

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

Q_list = []
for _ in range(Q):
    a, b, c, d = list(map(int, input().split()))
    Q_list.append((a, b, c, d))

A_list = []
for a in itertools.combinations_with_replacement(list(range(1, M+1)), N):
    A_list.append(a)

answer = 0
score_list = []
for A in A_list:
    score = 0
    for i in range(Q):
        a, b, c, d = Q_list[i]
        a -=1
        b -=1
        if A[b] - A[a] == c:
            score += d
    answer = max(answer, score)

print(answer)
# Original solution
from itertools import combinations_with_replacement

n, m, q = list(map(int, input().split()))
cond = []
for _ in range(q):
    a, b, c, d = list(map(int, input().split()))
    a -= 1
    b -= 1
    cond.append((a, b, c, d))


def calc(l):
    score = 0
    for a, b, c, d in cond:
        if l[b] - l[a] == c:
            score += d
    return score


ans = 0
for l in combinations_with_replacement(list(range(1, m + 1)), n):
    cand = calc(l)
    ans = max(ans, cand)

print(ans)


# Optimized solution
from itertools import combinations_with_replacement

n, m, q = list(map(int, input().split()))
cond = []
for _ in range(q):
    a, b, c, d = list(map(int, input().split()))
    a -= 1
    b -= 1
    cond.append((a, b, c, d))


def calc(l):
    score = 0
    for a, b, c, d in cond:
        if l[b] - l[a] == c:
            score += d
    return score


ans = 0
l = [0] * n
l[0] = 1


def dfs(l_len):
    global l, ans
    if l_len == n:
        ans = max(ans, calc(l))
        return

    for i in range(l[l_len - 1], m + 1):
        l[l_len] = i
        dfs(l_len + 1)


dfs(1)
print(ans)
# Original solution
import copy
n,m,q=[int(_) for _ in input().split()]
lista=[[int(i) for i in input().split()] for j in range(q)]
v=list()  #0,0,0,0,0....,0
def make(a,b,c): #a=list b=maxlen c=max
    global v
    if len(a)==b:
        v.append(a)
    else:
        for i in range(a[-1],c+1):
            k=copy.copy(a)
            k.append(i)
            make(k,b,c)
for t in range(1,m+1):
    make([t],n,m)
max=0
for j in v:
    point=0
    for l in lista:
        if j[l[1]-1]-j[l[0]-1]==l[2]:
            point+=l[3]
    if max<point:
        max=point
print(max)



# Optimized solution
import copy
n,m,q=[int(_) for _ in input().split()]
lista=[[int(i) for i in input().split()] for j in range(q)]
v=list()  #0,0,0,0,0....,0
def make(a,b,c): #a=list b=maxlen c=max
    global v
    if len(a)==b:
        v.append(a)
    else:
        for i in range(a[-1],c+1):
            make(a+[i],b,c)
for t in range(1,m+1):
    make([t],n,m)
max=0
for j in v:
    point=0
    for l in lista:
        if j[l[1]-1]-j[l[0]-1]==l[2]:
            point+=l[3]
    if max<point:
        max=point
print(max)
# Original solution
import math
from collections import deque
n,m,q=[int(_) for _ in input().split()]
lista=[[int(i) for i in input().split()] for j in range(q)]
v=deque()  #0,0,0,0,0....,0
def make(a,b,c): #a=list b=maxlen c=max
    global v
    if len(a)==b:
        v.append(a)
    else:
        for i in range(a[-1],c+1):
            make(a+deque([i]),b,c)
for t in range(1,m+1):
    make(deque([t]),n,m)
max=0
for j in v:
    point=0
    for l in lista:
        if j[l[1]-1]-j[l[0]-1]==l[2]:
            point+=l[3]
    if max<point:
        max=point
print(max)

# Optimized solution
from collections import deque
n,m,q=[int(_) for _ in input().split()]
lista=[[int(i) for i in input().split()] for j in range(q)]
v=deque()  #0,0,0,0,0....,0
def make(a,b,c): #a=list b=maxlen c=max
    global v
    if len(a)==b:
        v.append(a)
    else:
        for i in range(a[-1],c+1):
            make(a+[i],b,c)
for t in range(1,m+1):
    make([t],n,m)
max=0
for j in v:
    point=0
    for l in lista:
        if j[l[1]-1]-j[l[0]-1]==l[2]:
            point+=l[3]
    if max<point:
        max=point
print(max)
# Original solution
import itertools

N, M, Q = list(map(int, input().split()))
ABCD = [[int(x) for x in input().split()] for i in range(Q)]

ans=0
for A in itertools.combinations_with_replacement([m for m in range(1,M+1)], N):
  ans2=0
  for (a,b,c,d) in ABCD:
    if A[b-1]-A[a-1]==c:
      ans2+=d
  ans=max(ans, ans2)
print(ans)


# Optimized solution
import itertools

N, M, Q = list(map(int, input().split()))
ABCD = [[int(x) for x in input().split()] for i in range(Q)]

ans=0
t=[m for m in range(1,M+1)]
for A in itertools.combinations_with_replacement(t, N):
  ans2=0
  for (a,b,c,d) in ABCD:
    if A[b-1]-A[a-1]==c:
      ans2+=d
  ans=max(ans, ans2)
print(ans)
# Original solution
n, m, q = list(map(int, input().split()))
l = []
for i in range(q):
    l1, l2, l3, l4 = list(map(int, input().split()))
    l.append([l1, l2, l3, l4])
B = []
for a in range(1, m + 1):
    for b in range(a, m + 1):
        for c in range(b, m + 1):
            for d in range(c, m + 1):
                for e in range(d, m + 1):
                    for f in range(e, m + 1):
                        for g in range(f, m + 1):
                            for h in range(g, m + 1):
                                for i in range(h, m + 1):
                                    for j in range(i, m + 1):
                                        L = [a, b, c, d, e, f, g, h, i, j]
                                        A = []
                                        for I in range(n):
                                            A.append(L[I])
                                        C = 0
                                        for k in range(q):
                                            if A[l[k][1] - 1] - A[l[k][0] - 1] == l[k][2]:
                                                C += l[k][3]
                                        B.append(C)
print((max(B)))

# Optimized solution
def main():
    n, m, q = list(map(int, input().split()))
    l = []
    for i in range(q):
        l1, l2, l3, l4 = list(map(int, input().split()))
        l.append([l1, l2, l3, l4])
    B = []
    for a in range(1, m + 1):
        for b in range(a, m + 1):
            for c in range(b, m + 1):
                for d in range(c, m + 1):
                    for e in range(d, m + 1):
                        for f in range(e, m + 1):
                            for g in range(f, m + 1):
                                for h in range(g, m + 1):
                                    for i in range(h, m + 1):
                                        for j in range(i, m + 1):
                                            L = [a, b, c, d, e, f, g, h, i, j]
                                            A = []
                                            for I in range(n):
                                                A.append(L[I])
                                            C = 0
                                            for k in range(q):
                                                if A[l[k][1] - 1] - A[l[k][0] - 1] == l[k][2]:
                                                    C += l[k][3]
                                            B.append(C)
    print((max(B)))
    
main()
# Original solution
N,M,Q = list(map(int,input().split()))
abcd = [list(map(int,input().split())) for i in range(Q)]
ans = 0
for a in range(1,M+1):
    for b in range(a,M+1):
        for c in range(b,M+1):
            for d in range(c,M+1):
                for e in range(d,M+1):
                    for f in range(e,M+1):
                        for g in range(f,M+1):
                            for h in range(g,M+1):
                                for i in range(h,M+1):
                                    for j in range(i,M+1):
                                        lista = [a,b,c,d,e,f,g,h,i,j]
                                        ans1 = 0
                                        for A,B,C,D in abcd:
                                            if lista[B-1]-lista[A-1] == C:
                                                ans1 += D
                                        ans = max(ans,ans1)
print(ans)


# Optimized solution
N,M,Q = list(map(int,input().split()))
abcd = [list(map(int,input().split())) for i in range(Q)]
ans = 0
def dfs(n,A,now):
    global ans
    if n == 0:
        ansi = 0
        for a,b,c,d in abcd:
            if A[b-1]-A[a-1]==c:
                ansi+=d
        ans = max(ans,ansi)
    else:
        for i in range(now,M+1):
            dfs(n-1,A+[i],i)

A = []
dfs(N,A,1)
print(ans)
# Original solution
def main():
    n,m,q = list(map(int,input().split()))
    Q = []
    for _ in range(q):
        a,b,c,d = list(map(int,input().split()))
        Q.append([a,b,c,d])
    ans = 0
    for a in range(1,11):
        for b in range(a,11):
            for c in range(b,11):
                for d in range(c,11):
                    for e in range(d,11):
                        for f in range(e,11):
                            for g in range(f,11):
                                for h in range(g,11):
                                    for i in range(h,11):
                                        for j in range(i,11):
                                            l = [a,b,c,d,e,f,g,h,i,j]
                                            l = l[0:n]
                                            M = 0
                                            if max(l)>m:
                                                continue
                                            for q in Q:
                                                A,B,C,D = q[0],q[1],q[2],q[3]
                                                if l[B-1]-l[A-1] == C:
                                                    M += D
                                            if M>ans:
                                                ans = M
    print(ans)

    

if __name__ == "__main__":
    main()

# Optimized solution
def main():
    import itertools
    n,m,q = list(map(int,input().split()))
    Q = []
    for i in range(q):
        Q.append(list(map(int,input().split())))
    cmb = [i for i in range(1,m+1)]
    ans = 0
    for que in itertools.combinations_with_replacement(cmb,n):
        m = 0
        for i in range(q):
            [a,b,c,d] = Q[i]
            if que[b-1]-que[a-1]==c:
                m += d
        if m>ans:
            ans = m
    print(ans)

if __name__ == "__main__":
    main()
# Original solution
import copy
N, M, Q = [eval(v) for v in input().strip().split(" ")]

a_list, b_list, c_list, d_list = [], [], [], []

for k in range(Q):
    a, b, c, d = [eval(v) for v in input().strip().split(" ")]
    a_list.append(a)
    b_list.append(b)
    c_list.append(c)
    d_list.append(d)

max_value = 0

def calc_score(complete_A):
    score = 0
    for i in range(Q):
        if complete_A[b_list[i]] - complete_A[a_list[i]] == c_list[i]:
            score += d_list[i]
    return score

def search(A, M, depth, a_list, b_list, c_list, d_list, max_value, count):
    # print("%s: %s" % (depth, A))
    # count += 1
    # if count > 20:
    #     exit(0)
    if depth > 0:
        last_A = A[-1]
        for k in range(last_A, M + 1):
            next_A = copy.deepcopy(A)
            next_A.append(k)
            max_value = search(next_A, M, depth - 1, a_list, b_list, c_list, d_list, max_value, count)
    else:
        value = calc_score(A[1:])
        if value > max_value:
            max_value = value
    return max_value

count = 0
A = [1]
print((search(A, M, N + 1, a_list, b_list, c_list, d_list, max_value, count)))

# Optimized solution
# import copy
N, M, Q = [eval(v) for v in input().strip().split(" ")]

a_list, b_list, c_list, d_list = [], [], [], []

for k in range(Q):
    a, b, c, d = [eval(v) for v in input().strip().split(" ")]
    a_list.append(a)
    b_list.append(b)
    c_list.append(c)
    d_list.append(d)

max_value = 0

def calc_score(complete_A):
    score = 0
    for i in range(Q):
        if complete_A[b_list[i]] - complete_A[a_list[i]] == c_list[i]:
            score += d_list[i]
    return score

def search(A, M, depth, a_list, b_list, c_list, d_list, max_value, count):
    # print("%s: %s" % (depth, A))
    # count += 1
    # if count > 20:
    #     exit(0)
    if depth > 0:
        last_A = A[-1]
        for k in range(last_A, M + 1):
            # next_A = copy.deepcopy(A)
            # next_A.append(k)
            next_A = A + [k]
            max_value = search(next_A, M, depth - 1, a_list, b_list, c_list, d_list, max_value, count)
    else:
        value = calc_score(A[1:])
        if value > max_value:
            max_value = value
    return max_value

count = 0
A = [1]
print((search(A, M, N + 1, a_list, b_list, c_list, d_list, max_value, count)))
# Original solution
n,m,q=list(map(int,input().split()))
abcd=[[0]*4 for i in range(q)]
for i in range(q):
    abcd[i]=list(map(int,input().split()))

def dfs(nn,anni,vmax):
    if nn==n:
        qsum=0
        for j in range(q):
#            print(j,abcd[j][1],abcd[j][0])
            if a[abcd[j][1]-1]-a[abcd[j][0]-1]==abcd[j][2]:
                qsum=qsum+abcd[j][3]
#        print(qsum,a)
        return qsum
    a[nn]=anni
#    vmax=0
    for i in range(10):
        if a[nn]+i<=m:
            v=dfs(nn+1,a[nn]+i,vmax)
            vmax=max(v,vmax)
    return vmax

vmax=0
a=[0]*n
print((dfs(0,1,vmax)))


# Optimized solution
from itertools import combinations_with_replacement

n,m,q=list(map(int,input().split()))
abcd=[]
for i in range(q):
    abcd.append(list(map(int,input().split())))

ans=0
for A in combinations_with_replacement(list(range(1,m+1)),n):
#    print(A)
    asum=0
    for a,b,c,d in abcd:
        if A[b-1]-A[a-1]==c:
            asum+=d
    ans=max(ans,asum)
print(ans)
# Original solution
import itertools
from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, list(range(n, n - r, -1)), 1)
    denom = reduce(mul, list(range(1, r + 1)), 1)
    return numer // denom
  
N,M,Q=list(map(int,input().split()))
a=[0]*Q;b=[0]*Q;c=[0]*Q;d=[0]*Q
for i in range(Q):
  a[i],b[i],c[i],d[i]=list(map(int,input().split()))
l=list(range(1,M+1))


h=combinations_count(M+N-1, N)
H=[0]*h;i=0
for v in itertools.combinations_with_replacement(l, N):
    H[i]=v
    i+=1

D=[0 for j in range(h)]

for i in range(h):
  for j in range(Q):
    if H[i][b[j]-1]-H[i][a[j]-1]==c[j]:
      D[i]+=d[j]
  
print((max(D)))


# Optimized solution
import itertools
N,M,Q=list(map(int,input().split()))
a=[0]*Q;b=[0]*Q;c=[0]*Q;d=[0]*Q
for i in range(Q):
  a[i],b[i],c[i],d[i]=list(map(int,input().split()))
l=list(range(1,M+1))

i=0;D=[]
for v in itertools.combinations_with_replacement(l, N):
  D.append(0)
  for j in range(Q):
    if v[b[j]-1]-v[a[j]-1]==c[j]:
      D[i]+=d[j]
  i+=1
print((max(D)))