CodeEff/ECCO huggingface dataset:
# Original solution
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M, Q = lr()
ABCD = [lr() for _ in range(Q)]
A = [1] * N
ABCD.sort(key=lambda x: x[3], reverse=True)
ABCD.sort(key=lambda x: x[0])
answer = 0
for a in [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):
                                        temp = 0
                                        x = [a,b,c,d,e,f,g,h,i,j]
                                        for k, l, m, n in ABCD:
                                            if x[l-1] - x[k-1] == m:
                                                temp += n
                                        if temp > answer:
                                            answer = temp

print(answer)


# Optimized solution
# coding: utf-8
import sys
from itertools import combinations_with_replacement

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M, Q = lr()
ABCD = [lr() for _ in range(Q)]
answer = 0
for x in combinations_with_replacement(list(range(1, M+1)), N):
    temp = 0
    for a, b, c, d in ABCD:
        if x[b-1] - x[a-1] == c:
            temp += d
    if temp > answer:
        answer = temp

print(answer)
#05
# Original solution
from sys import stdin
from itertools import combinations_with_replacement

input = stdin.readline
N,M,Q = list(map(int, input().split()))
numlist = [i for i in range(1,M+1)]
A = [0]*(Q+1)
B = [0]*(Q+1)
C = [0]*(Q+1)
D = [0]*(Q+1)
for q in range(1,Q+1):
    A[q],B[q],C[q],D[q] = list(map(int, input().split()))

maxscore = 0
for tpl in combinations_with_replacement(numlist,N):
    score = 0
    for q in range(1,Q+1):
        if tpl[B[q]-1] - tpl[A[q]-1] == C[q]:
            score += D[q]
    maxscore = max(maxscore, score)
print(maxscore)

# Optimized solution
from itertools import combinations_with_replacement
from sys import stdin
input = stdin.readline

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()))

ans = 0
X = [i for i in range(1,M+1)]
for tup in combinations_with_replacement(X,N):
    total = 0
    for i in range(Q):
        if tup[B[i]-1] - tup[A[i]-1] == C[i]:
            total += D[i]
    ans = max(ans,total)

print(ans)
# Original solution
N, M, Q = list(map(int, input().split()))  #数字
Q = [list(map(int, input().split())) for i in range(Q)]  #数字複数列、複数行

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

B = []
import itertools
for i in itertools.combinations_with_replacement(A,N):
    B.append(i)

C = []
for i in range(len(B)):
    score = 0
    for j in range(len(Q)):
        if B[i][Q[j][1]-1] - B[i][Q[j][0]-1] == Q[j][2]:
            score += Q[j][3]
        C.append(score)

print((max(C)))

# Optimized solution
N, M, Q = list(map(int, input().split()))  #数字
Q = [list(map(int, input().split())) for i in range(Q)]  #数字複数列、複数行

import itertools
A = []
for i in itertools.combinations_with_replacement(list(range(M)),N):
    score = 0
    for j in range(len(Q)):
        if i[Q[j][1]-1] - i[Q[j][0]-1] == Q[j][2]:
            score += Q[j][3]
        A.append(score)

print((max(A)))
# Original solution
N, M, Q = list(map(int, input().split()))  #数字
Q = [list(map(int, input().split())) for i in range(Q)]  #数字複数列、複数行

import itertools
A = []
for i in itertools.combinations_with_replacement(list(range(M)),N):
    score = 0
    for j in range(len(Q)):
        if i[Q[j][1]-1] - i[Q[j][0]-1] == Q[j][2]:
            score += Q[j][3]
        A.append(score)

print((max(A)))

# Optimized solution
N, M, Q = list(map(int, input().split()))  #数字
Q = [list(map(int, input().split())) for i in range(Q)]  #数字複数列、複数行

import itertools
A = 0
for i in itertools.combinations_with_replacement(list(range(M)),N):
    score = 0
    for j in range(len(Q)):
        if i[Q[j][1]-1] - i[Q[j][0]-1] == Q[j][2]:
            score += Q[j][3]
        if score > A:
            A = score

print(A)
# Original solution
# -*- coding: utf-8 -*-
import sys
from collections import deque
import copy

N,M,Q=list(map(int, sys.stdin.readline().split()))
X=[ list(map(int, sys.stdin.readline().split())) for _ in range(Q) ]

qq=deque()
for m in range(1,M+1):
    q=deque()
    q.append([m])
    while q:
        if N<len(q[0]):
            qq+=q
            break
        fro=q.popleft()
        last=fro[-1]
        for to in range(last,M+1):
            q.append(fro+[to])

ans=float("-inf")
for L in qq:
    temp_ans=0
    for a,b,c,d in X:
        if L[b-1]-L[a-1]==c:
            temp_ans+=d
    ans=max(ans,temp_ans)
print(ans)


# Optimized solution
# -*- coding: utf-8 -*-
import sys
from collections import deque

N,M,Q=list(map(int, sys.stdin.readline().split()))
X=[ list(map(int, sys.stdin.readline().split())) for _ in range(Q) ]

qq=deque()
for m in range(1,M+1):
    q=deque()
    q.append([m])
    while q:
        if N<len(q[0]):
            qq+=q
            break
        fro=q.popleft()
        last=fro[-1]
        for to in range(last,M+1):
            q.append(fro+[to])

ans=float("-inf")
for L in qq:
    temp_ans=0
    for a,b,c,d in X:
        if L[b-1]-L[a-1]==c:
            temp_ans+=d
    ans=max(ans,temp_ans)
print(ans)
# Original solution
from itertools import combinations_with_replacement
n, m, q = list(map(int, input().split()))
s_l = []
for i in range(q):
    s = list(map(int, input().split()))
    s_l.append(s)

A = []
for v in combinations_with_replacement(list(range(1,m+1)), n):
    A.append(v)

ans = 0
for a in A:
    cnt = 0
    for i in range(q):
        if a[s_l[i][1] - 1] - a[s_l[i][0] - 1] == s_l[i][2]:
            cnt += s_l[i][3]
    ans = max(ans, cnt)

print(ans)

# Optimized solution
from itertools import combinations_with_replacement
n, m, q = list(map(int, input().split()))
s_l = []
for i in range(q):
    s = list(map(int, input().split()))
    s_l.append(s)

ans = 0
for v in combinations_with_replacement(list(range(1, m+1)), n):
    cnt = 0
    for i in range(q):
        if v[s_l[i][1] - 1] - v[s_l[i][0] - 1] == s_l[i][2]:
            cnt += s_l[i][3]
    ans = max(ans, cnt)

print(ans)
# Original solution
import itertools
N, M, Q = list(map(int, input().split()))
l = []
for _ in range(Q):
    l.append(tuple(map(int, input().split())))
    
c = list(itertools.combinations(list(range(1, M+1))*Q, N))

max = 0
for i in c:
    i = list(i)
    i.sort()
    sum = 0
    for j in l:
        if i[j[1]-1] - i[j[0]-1] == j[2]:
            sum += j[3]
    if max < sum:
        max = sum
        
print(max)

# Optimized solution
import itertools
N, M, Q = list(map(int, input().split()))
l = []
for _ in range(Q):
    l.append(tuple(map(int, input().split())))
    
c = list(itertools.combinations_with_replacement(list(range(1, M+1)), N))

m = 0
for i in c:
    flag = True
    sum = 0
    for j in l:
        if i[j[1]-1] - i[j[0]-1] == j[2]:
            sum += j[3]
    m = max(m, sum)
        
print(m)
# Original solution
import itertools
n,m,q=list(map(int, input().split()))
 
tmp = 0
max = 0
c = []
 
for i in range(q):
    c.append([int(j) for j in input().split()])
 
nums = list(range(1, m + 1))
for retsu in itertools.combinations_with_replacement(nums, n): 
    for i in range(q):
        if int(retsu[c[i][1]-1]) - int(retsu[c[i][0]-1]) == c[i][2]:
            tmp = tmp + c[i][3]
        if max < tmp:
            max = tmp
    tmp = 0
 
print(max)

# Optimized solution
import itertools
n,m,q=list(map(int, input().split()))
 
tmp = 0
max = 0
a = []
b = []
c = []
d = []
 
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)
 
nums = list(range(1, m + 1))
 
for retsu in itertools.combinations_with_replacement(nums, n): 
    for i in range(q):
        if int(retsu[b[i]-1]) - int(retsu[a[i]-1]) == c[i]:
            tmp = tmp + d[i]
        if max < tmp:
            max = tmp
    tmp = 0
 
print(max)
# Original solution
n, m, q = list(map(int, input().split()))


abcdList = []
for _ in range(q):
  abcd = list(map(int, input().split()))
  abcdList.append(abcd)
aList = []
def createA(tempA, x, t):
  if x!=0:
      tempA.append(t)
  if x == n:
    abc = []
    for item in tempA:
        abc.append(item)
    aList.append(abc)
    return
  x += 1
  for i in range(t, m+1):
    if x == 1:
        tempA = []
    createA(tempA, x, i)
    tempA.pop(-1)



x = 0
createA([], x, 1)
maxAns = 0
for item in aList:
  ans = 0
  for i in range(q):
    b = abcdList[i][1]
    a = abcdList[i][0]
    c = abcdList[i][2]
    d = abcdList[i][3]
    if item[b-1] - item[a-1] == c:
      ans += d
  if ans > maxAns:
    maxAns = ans
print(maxAns)
    
  
  




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

abcd = [list(map(int, input().split())) for _ in range(q)]
from itertools import combinations_with_replacement as cwr
AList = list(cwr(list(range(1, m+1)), n))


maxPoint = 0
for A in AList:
  point = 0
  for abc in abcd:
    a = abc[0]
    b = abc[1]
    c = abc[2]
    d = abc[3]
    if A[b-1] - A[a-1] == c:
      point += d
  if point > maxPoint:
    maxPoint = point
print(maxPoint)
# Original solution
N,M,Q=list(map(int,input().split()))
from itertools import combinations_with_replacement
a=[i for i in range(1,M+1)]
A=list(combinations_with_replacement(a,N))
#print(A)
ans=[]
for i in range(Q):
    W,X,Y,Z=list(map(int,input().split()))
    #print(X,W)
    for j in A:
        if j[X-1]-j[W-1]==Y:
            ans.append(Z)
        else:
            ans.append(0)
#print(ans)
C=len(A)
Ans=[]

for i in range(C):
    tmp=0
    F=ans[i::C]
    tmp=sum(F)
    Ans.append(tmp)
#print(Ans)
print((max(Ans)))


# Optimized solution
from itertools import permutations, combinations,combinations_with_replacement,product
N,M,Q=list(map(int,input().split()))
a=[i for i in range(1,M+1)]
A=list(combinations_with_replacement(a,N))
B=[list(map(int,input().split())) for i in range(Q)]
ans=[]
for i in A:
    tmp=0
    #print(i)
    for j in B:
        #print(j)
        if i[j[1]-1]-i[j[0]-1]==j[2]:
            tmp+=j[3]
        else:
            pass
    ans.append(tmp)
print((max(ans)))
# Original solution
from collections import deque
from itertools import combinations_with_replacement as combi

N, M, Q = list(map(int, input().split()))
road = deque([input().split() for i in range(Q)])
l = [i+1 for i in range(M)]
point = 0
for v in combi(l, N):
    tmp = 0
    for j in range(Q):
        a, b, c, d = list(map(int, road[j]))
        if v[b-1] - v[a-1] == c:
            tmp += d
    point = max(point, tmp)

print(point)

# Optimized solution
from itertools import combinations_with_replacement as combi

N, M, Q = list(map(int, input().split()))
a = [0] * Q
b = [0] * Q
c = [0] * Q
d = [0] * Q
l = [i+1 for i in range(M)]
point = 0

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

for v in combi(l, N):
    tmp = 0
    for j in range(Q):
        if v[b[j]-1] - v[a[j]-1] == c[j]:
            tmp += d[j]
    point = max(point, tmp)

print(point)
# Original solution
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list([x-1 for x in MII()])
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def to_bin(x: int) -> str: return format(x, 'b') # rev => int(res, 2)
def to_oct(x: int) -> str: return format(x, 'o') # rev => int(res, 8)
def to_hex(x: int) -> str: return format(x, 'x') # rev => int(res, 16)
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]
## libs ##
from itertools import accumulate as acc
from itertools import combinations_with_replacement as combi
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#

def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]
    # l = [i for i in range(1,m+1)]
    # maxv = 0
    # for v in combi(l,n):
    #     sumv = 0
    #     for a,b,c,d in query:
    #         if v[b-1]-v[a-1] == c:
    #             sumv += d
    #     maxv = max(maxv, sumv)
    # print(maxv)
    def score(A):
        res = 0
        for a, b, c, d in query:
            if A[b-1]-A[a-1] == c:
                res += d
        return res

    def dfs(A):
        if len(A) == n:
            return score(A)
        res = 0
        prev_last = A[-1] if len(A) > 0 else 0
        for v in range(prev_last, m):
            A.append(v)
            res = max(res, dfs(A))
            A.pop()
        return res
    print((dfs([])))


if __name__ == '__main__':
    main()

# Optimized solution
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
#======================================================#

def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]

    def calc_point(lst):
        sumv = 0
        for a, b, c, d in query:
            if lst[b-1] - lst[a-1] == c:
                sumv += d
        return sumv

    def dfs(lst):
        if len(lst) == n:
            return calc_point(lst)
        point = 0
        last_elm = lst[-1]
        for i in range(last_elm, m+1):
            point = max(point, dfs(lst+[i]))
        return point

    print((dfs([1])))


if __name__ == '__main__':
    main()
# Original solution
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
#======================================================#

def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]

    def calc_point(lst):
        sumv = 0
        for a, b, c, d in query:
            if lst[b-1] - lst[a-1] == c:
                sumv += d
        return sumv

    def dfs(lst):
        if len(lst) == n:
            return calc_point(lst)
        point = 0
        last_elm = lst[-1] if len(lst) > 0 else 1
        for i in range(last_elm, m+1):
            point = max(point, dfs(lst+[i]))
        return point

    print((dfs([])))


if __name__ == '__main__':
    main()

# Optimized solution
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
#======================================================#

def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]

    def calc_point(lst):
        sumv = 0
        for a, b, c, d in query:
            if lst[b-1] - lst[a-1] == c:
                sumv += d
        return sumv

    def dfs(lst):
        len_lst = len(lst)
        if len_lst == n:
            return calc_point(lst)
        point = 0
        last_elm = lst[-1] if len_lst > 0 else 1
        for i in range(last_elm, m+1):
            point = max(point, dfs(lst+[i]))
        return point

    print((dfs([])))


if __name__ == '__main__':
    main()
# Original solution
from itertools import combinations_with_replacement
N, M, Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for x in combinations_with_replacement(list(range(1, M+1)), N):
    point = 0
    for a, b, c, d in abcd:
        if x[b-1] - x[a-1] == c:
            point += d
    if ans < point:
        ans = point
print(ans)


# Optimized solution
from itertools import combinations_with_replacement
def main():
    N, M, Q = list(map(int, input().split()))
    abcd = [list(map(int, input().split())) for _ in range(Q)]
    ans = 0
    for x in combinations_with_replacement(list(range(1, M+1)), N):
        point = 0
        for a, b, c, d in abcd:
            if x[b-1] - x[a-1] == c:
                point += d
        if ans < point:
            ans = point
    print(ans)

if __name__ == '__main__':
    main()
# Original solution
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

def cal(x):
    rr = 0
    for i in range(Q):
        a,b,c,d = query[i]
        if x[b] - x[a] == c:
            rr += d
    return rr

n,m,Q = inpl()
q = deque()
q.append([1])
query = []
for _ in range(Q):
    query.append(inpl())
res = 0
while q:
    now = q.popleft()
    if len(now) == n+1:
        res = max(res, cal(now))
        continue
    for i in range(now[-1],m+1):
        tmp = now + [i]
        q.append(tmp)
print(res)

# Optimized solution
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))


n,m,q = inpl()
abc = [inpl() for _ in range(q)]
q = deque()
q.append([1])
res = 0
while q:
    now = q.popleft()
    if len(now) == n+1:
        # print(now)
        ans = 0
        for i,x in enumerate(abc):
            a,b,c,d = x
            if now[b] - now[a] == c:
                ans += d
        res = max(ans, res)
        continue
    for i in range(now[-1], m+1):
        q.append(now + [i])
print(res)
# Original solution
def solve():
    N,M,Q = list(map(int,input().split()))
    req = []
    for i in range(Q):
        a,b,c,d = list(map(int,input().split()))
        a -= 1
        b -= 1
        req.append([a,b,c,d])

    res = []
    dfs(list(), res,M,N)

    mx = 0
    for p in res:
        point = 0
        for a,b,c,d in req:
            if p[b] - p[a] == c:
                point += d
        mx = max(point, mx)
    
    print(mx)


def dfs(now,res,M,N):
    if len(now) == N:
        res.append(now)
        return
    start = 1
    if len(now) != 0:
        start = now[-1]

    if start > M:
        return

    for i in range(start,M+1):
        nxt = now[:]
        nxt.append(i)
        dfs(nxt, res,M,N)
    
if __name__ == '__main__':
    solve()

# Optimized solution
def solve():
    N,M,Q = list(map(int,input().split()))
    global info
    info = []
    for _ in range(Q):
        a,b,c,d = list(map(int,input().split()))
        a -= 1
        b -= 1
        info.append([a,b,c,d])

    print((dfs(list(),M,N)))

def dfs(A,M,N):
    if len(A) == N:
        return calcPoint(A)
    
    res = 0
    prev = A[-1] if len(A) > 0 else 1
    for i in range(prev,M+1):
        A.append(i)
        res = max(res,dfs(A,M,N))
        A.pop()
    
    return res

def calcPoint(A):
    result = 0
    for a,b,c,d in info:
        if A[b] - A[a] == c:
            result += d
    return result
                
if __name__ == '__main__':
    solve()
# Original solution
from itertools import combinations
import sys
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(20000000)

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

# def check(Q):
#     A = [1] + [-1 for _ in range(n-1)]
#     score = 0
#     for a, b, c, d in Q:
#         if A[a] == -1 and A[b] == -1:
#             tmp_b = min(A[b:])
#             if tmp_b == -1 or tmp_b >= max(A[:a]) + c:
#                 A[a] = max(A[:a])
#                 A[b] = A[a] + c
#                 score += d
#         elif A[a] == -1 and A[b] != -1:
#             if max(A[:a]) <= A[b] - c <= max(A[:a]):
#                 A[a] = A[b] - c
#                 score += d
#         elif A[a] != -1 and A[b] == -1:
#             if min(A[:b]) <= A[a] + c <= max(A[b:]):
#                 A[b] = A[a] + c
#                 score += d
#         else:
#             if A[b] - A[a] == c:
#                 score += d
#         print(A)
#     return score

def dfs(A, depth, max_a):
    if depth == n:
        score = 0
        for a, b, c, d in abcd:
            if A[b] - A[a] == c:
                score += d
        return score
    ans = 0
    for i in range(max_a, m+1):
        ans = max(ans, dfs(A + [i], depth+1, i))
    return ans

ans = dfs([1], 1, 1)
print(ans)



# Optimized solution
from itertools import combinations
import sys
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(20000000)

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

def dfs(A, depth, max_a):
    if depth == n:
        score = 0
        for a, b, c, d in abcd:
            if A[b] - A[a] == c:
                score += d
        return score
    ans = 0
    for i in range(max_a, m+1):
        ans = max(ans, dfs(A + [i], depth+1, i))
    return ans

ans = dfs([1], 1, 1)
print(ans)
# Original solution
import sys,itertools
input = sys.stdin.readline

n,m,q = list(map(int,input().split()))
seq = list(range(n+m-1))
p = list(itertools.combinations(seq,n))

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()))

res = 0
for i in range(len(p)):
    t = [0]*n
    pp = sorted(p[i])
    idx = 0
    for e in pp:
        t[idx] = 1+e-idx
        idx += 1
    
    tmp = 0
    for j in range(q):
        if t[b[j]-1] - t[a[j]-1] == c[j]:
            tmp += d[j]
    
    res = max(res,tmp)

print(res)


# Optimized solution
import itertools

n, m, q = list(map(int,input().split()))
seq = list(range(n+m-1))
p = list(itertools.combinations(seq, n))

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()))

res = 0
for i in range(len(p)):
    t = [0] * n
    pp = sorted(p[i])
    idx = 0
    for e in pp:
        t[idx] = 1 + e - idx
        idx += 1
    
    tmp = 0
    for j in range(q):
        if t[b[j]-1] - t[a[j]-1] == c[j]:
            tmp += d[j]
    
    res = max(res, tmp)

print(res)
# Original solution
# 重複組み合わせの全列挙

import itertools

# n+m-1個からn個を選ぶ組み合わせ
n, m, q = list(map(int, input().split()))
seq = list(range(n+m-1))
p = list(itertools.combinations(seq, n))

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()))

res = 0
for i in p:
    t = [0] * n
    pp = sorted(i)
    idx = 0
    for e in pp:
        t[idx] = 1 + e - idx
        idx += 1
    
    tmp = 0
    for j in range(q):
        if t[b[j]-1] - t[a[j]-1] == c[j]:
            tmp += d[j]
    
    res = max(res, tmp)

print(res)

# Optimized solution
# 重複組み合わせの全列挙

import itertools

# n+m-1個からn個を選ぶ組み合わせ
n, m, q = list(map(int, input().split()))
seq = list(range(n+m-1))
p = list(itertools.combinations(seq, n))

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()))

res = 0
for i in p:
    t = [0] * n
    idx = 0
    for e in i:
        t[idx] = 1 + e - idx
        idx += 1
    
    tmp = 0
    for j in range(q):
        if t[b[j]-1] - t[a[j]-1] == c[j]:
            tmp += d[j]
    
    res = max(res, tmp)

print(res)