CodeEff/ECCO huggingface dataset:
# Original 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()))
    
A=list(itertools.combinations_with_replacement([i for i in range(1,m+1)],n))


g=[]
for v in A:
    e=0
    for i in range(q):
        
        if v[b[i]-1]-v[a[i]-1] ==c[i]:
            e += d[i]
            
        
    g += [e]
print((max(g)))

# Optimized solution
from itertools import combinations_with_replacement

n,m,q =list(map(int,input().split()))
summa=[]
res=[]
for i in range(q):
    a=[int(i) for i in input().split()]
    summa.append(a)
    
for i in combinations_with_replacement(list(range(1,m+1)),n):
    cnt=0
    for j in summa:
            if i[j[1]-1]-i[j[0]-1]==j[2]:
                cnt += j[3]
            
    res.append(cnt)
    
print((max(res)))
# Original solution
from itertools import*
n,m,q,*L=list(map(int,open(0).read().split()))
print((max(sum(d*(x[b-1]-x[a-1]==c)for a,b,c,d in zip(*[iter(L)]*4))for x in combinations_with_replacement(list(range(m)),n))))

# Optimized solution
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.buffer.readline
n,m,q=list(map(int,input().split()))
Q=[list(map(int,input().split())) for _ in range(q)]
ans=0
def dfs(i,tab,ma):
	global ans
	if i==n:
		t=0
		for a,b,c,d in Q:
			if tab[b-1]-tab[a-1]==c:
				t+=d
		if t>ans:
			ans=t
		return
	for j in range(ma,m+1):
		dfs(i+1,tab+[j],j)
dfs(0,[],1)
print(ans)
# Original solution

from itertools import combinations_with_replacement

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

x = [_ for _ in range(1, M+1)]
#y = list(combinations(x, N))
y = list(combinations_with_replacement(x, N))

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

ans = 0
z = 0
for i in range(len(y)):
    for j in range(Q):
        if y[i][l[j][1]-1] - y[i][l[j][0]-1] == l[j][2]:
            z += l[j][3]
        ans = max(ans, z)
    z = 0
print(ans)
    
            
    
    

# Optimized solution
from itertools import combinations_with_replacement

N, M, Q = list(map(int,input().split()))
x = [_ for _ in range(1, M+1)]
y = list(combinations_with_replacement(x, N))
l = [list(map(int, input().split())) for _ in range(Q)]

ans = []
z = 0
for i in range(len(y)):
    for j in range(Q):
        if y[i][l[j][1]-1] - y[i][l[j][0]-1] == l[j][2]:
            z += l[j][3]
        ans.append(z)
    z = 0
print((max(ans)))
# Original solution
from itertools import combinations_with_replacement

N, M, Q = list(map(int,input().split()))
x = [_ for _ in range(1, M+1)]
y = list(combinations_with_replacement(x, N))
l = [tuple(map(int, input().split())) for _ in range(Q)]

ans = 0
z = 0
for i in range(len(y)):
    for j in range(Q):
        if y[i][l[j][1]-1] - y[i][l[j][0]-1] == l[j][2]:
            z += l[j][3]
        ans = max(ans, z)
    z = 0
print(ans)

# Optimized solution
from itertools import combinations_with_replacement

N, M, Q = list(map(int,input().split()))
x = [_ for _ in range(1, M+1)]
y = list(combinations_with_replacement(x, N))
l = [list(map(int, input().split())) for _ in range(Q)]

ans = 0
z = 0
for i in range(len(y)):
    for j in range(Q):
        if y[i][l[j][1]-1] - y[i][l[j][0]-1] == l[j][2]:
            z += l[j][3]
        ans = max(ans, z)
    z = 0
print(ans)
# Original solution
#実験
# -*- 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)

# Optimized solution
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)))
# Original solution
import sys
input = sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log
from heapq import heapify, heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from decimal import Decimal
inf = float('inf')
MOD = 1000000007
def INT_(n): return int(n) - 1
def MI(): return list(map(int, input().split()))
def MF(): return list(map(float, input().split()))
def MI_(): return list(map(INT_,input().split()))
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n: int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in eval(input())]
def I(): return int(eval(input()))
def F(): return float(eval(input()))
def ST(): return input().replace('\n', '')

n, m, q = MI()
abcd = LLIN(q)

arr = []
def dfs(A: list):
    if len(A) == n:
        arr.append(A)
        return
    
    if len(A) == 0:
        s = 1
    else:
        s = A[-1]
    
    while s <= m:
        dfs(A + [s])
        s += 1
       

def main():
    dfs([])
    ans = 0
    for A in arr:
        tmp = 0
        for a,b,c,d in abcd:
            if A[b-1] - A[a-1] == c:
                tmp += d
        ans = max(ans, tmp)
    
    print(ans)

if __name__ == "__main__":
    main()


# Optimized solution
import sys
input = sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log
from heapq import heapify, heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from decimal import Decimal
inf = float('inf')
MOD = 1000000007
def INT_(n): return int(n) - 1
def MI(): return list(map(int, input().split()))
def MF(): return list(map(float, input().split()))
def MI_(): return list(map(INT_,input().split()))
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n: int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in eval(input())]
def I(): return int(eval(input()))
def F(): return float(eval(input()))
def ST(): return input().replace('\n', '')

n, m, q = MI()
abcd = LLIN(q)

def main():
    data = list(range(1, m+1))
    arr = combinations_with_replacement(data, n)
    ans = 0
    for A in arr:
        tmp = 0
        for a,b,c,d in abcd:
            if A[b-1] - A[a-1] == c:
                tmp += d
            
        ans = max(ans, tmp)
    
    print(ans)
    

if __name__ == "__main__":
    main()
# Original solution
N,M,Q = list(map(int,input().split()))
abcd = [list(map(int,input().split())) for i in range(Q)]

import itertools

tmp = []
for i in range(1,M+1):
    tmp.append(i)
A = list(itertools.combinations_with_replacement(tmp,N))
tmp2 = []
for i in range(len(A)):
    B = list(A[i])
    B.sort()
    tmp2.append(B)
answer = 0


for i in range(len(A)):
    ans = 0
    for j in range(Q):
        if tmp2[i][ abcd[j][1]-1 ] - tmp2[i][ abcd[j][0]-1 ] == abcd[j][2]:
            ans += abcd[j][3]  
    answer = max(answer,ans)
    
print(answer)

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

import itertools

A = list(itertools.combinations_with_replacement(list(range(1,M+1)),N))
answer = 0

for i in range(len(A)):
    ans = 0
    for j in range(Q):
        if A[i][ abcd[j][1]-1 ] - A[i][ abcd[j][0]-1 ] == abcd[j][2]:
            ans += abcd[j][3]  
    answer = max(answer,ans)
    
print(answer)
# Original solution
def comb(n,r):
    r=min(r,n-r)
    c=1
    for i in range(r):
        c=c*(n-i)/(r-i)
    return int(c)

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

npat=comb(N+M,M)
A=[1 for _ in range(N)]
ans=0

for i in range(npat):
    temp=0
    for j in range(Q):
        if A[a[j][1]-1]-A[a[j][0]-1]==a[j][2]:
            temp+=a[j][3]
    ans=max(ans,temp)
#    print(A)
#    print(temp)
    A[-1]+=1
    if A[-1]>M and i<npat-1:
        k=1
        while max(A)>M and k<N:
            A[N-k-1]+=1
            A[N-k]=A[N-k-1]
            k+=1
            for l in range(1,k):
                A[N-l]=A[N-k]
    if min(A)>M:
        break
print(ans)

# Optimized solution
def dfs(A):
    if len(A)==N:
        temp=0
        for j in range(Q):
            if A[a[j][1]-1]-A[a[j][0]-1]==a[j][2]:
                temp+=a[j][3]
        ans.append(temp)
        return
    else:
        if len(A)>0:
            m=A[-1]
        else:
            m=1
        for i in range(m,M+1):
            A.append(i)
            dfs(A)
            A.pop()
        
N,M,Q=list(map(int,input().split()))
a=[list(map(int,input().split())) for _ in range(Q)]
ans=[]
dfs([])
print((max(ans)))
# Original solution
n,m,q=list(map(int,input().split()))
abcd=[list(map(int,input().split())) for _ in range(q)]
all_a=[]
def make_a(t):
  ret=[]
  todo=[t]
  while todo:
    i=todo.pop()
    if len(i)==n:
      ret.append(i)
      continue
    for j in range(i[-1],m+1):
      todo.append(i+[j])
  return ret
for i in range(1,m+1):
  all_a.extend(make_a([1]))
ans=0
for an in all_a:
  ansi=0
  for a,b,c,d in abcd:
    if an[b-1]-an[a-1]==c:
      ansi+=d
  ans=max(ans,ansi)
print(ans)

# Optimized solution
n,m,q=list(map(int,input().split()))
abcd=[list(map(int,input().split())) for _ in range(q)]
all_a=[]
def make_a():
  ret=[]
  todo=[[]]
  while todo:
    i=todo.pop()
    if len(i)==n:
      ret.append(i)
      continue
    l=i[-1] if len(i)>0 else 1
    for j in range(l,m+1):
      todo.append(i+[j])
  return ret

all_a=make_a()
ans=0
for an in all_a:
  ansi=0
  for a,b,c,d in abcd:
    if an[b-1]-an[a-1]==c:
      ansi+=d
  ans=max(ans,ansi)
print(ans)
# Original solution
import itertools
N, M, Q = list(map(int, input().split()))
buff = [list(map(int, input().split())) for _ in range(Q)]

ans = 0
for A in itertools.combinations_with_replacement(list(range(1, M+1)), N):
    score = 0
    for a, b, c, d in buff:
        if A[b-1] - A[a-1] == c:
            score += d
    ans = max(ans, score)

print(ans)

# Optimized solution
# coding: utf-8
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

def dfs(A):
    # 再代入のため、グローバル宣言
    global ans
    
    # N桁なら計算
    if len(A) == N:
        score = 0
        for a, b, c, d in buff:
            if A[b-1] - A[a-1] == c:
                score += d
        ans = max(ans, score)
        return
    
    A.append(A[-1])
    # 再帰
    while A[-1] <= M:
        # コピーを渡す
        dfs(A[:])
        A[-1] += 1


# main
N, M, Q = list(map(int, input().split()))
buff = [list(map(int, input().split())) for _ in range(Q)]

ans = 0
dfs([1])

print(ans)
# Original solution
import sys
from copy import deepcopy
sys.setrecursionlimit(10**7)


def dfs(now):
    global ans
    if len(now) == N:
        tmp_ans = 0
        for a, b, c, d in query:
            a -= 1
            b -= 1
            if now[b] - now[a] == c:
                tmp_ans += d
        ans = max(tmp_ans, ans)
    else:
        for i in range(now[-1], M+1):
            tmp = deepcopy(now)
            tmp.append(i)
            dfs(tmp)


N, M, Q = list(map(int, input().split()))  # N個の数列, 上限M, Q個のクエリ
query = [list(map(int, input().split())) for _ in range(Q)]

A = [1]
ans = 0
dfs(A)
print(ans)


# Optimized solution
# 全探索で行う
N, M, Q = list(map(int, input().split()))
query = [list(map(int, input().split())) for _ in range(Q)]
A = [1]

def dfs(now):
    if len(now) == N:
        global ans
        tmp_ans = 0
        for a, b, c, d in query:
            a -= 1
            b -= 1
            if now[b] - now[a] == c:
                tmp_ans += d
        ans = max(ans, tmp_ans)
    else:
        for i in range(now[-1], M+1):
            tmp = now.copy()
            tmp.append(i)
            dfs(tmp)


ans = 0
dfs(A)
print(ans)
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
INF = 10**10
MOD = 10**9 + 7

        



N, M, Q  = list(map(int,input().split()))
query = []
for q in range(Q):
    a,b,c,d = list(map(int,input().split()))
    a-=1
    b-=1
    query.append((a,b,c,d))




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

for j in range(N-1):
    new = []
    for e in old:
        for i in range(e[-1],M+1):
            new.append(e+[i])
    old = new


#print (new)
mx = 0
for n in new:
    score = 0
    for a,b,c,d in query:
        if (n[b] - n[a] == c):
            score += d
    mx = max(score,mx)
print (mx)

# Optimized solution
import itertools
N, M, Q  = list(map(int,input().split()))
query = []
for q in range(Q):
    a,b,c,d = list(map(int,input().split()))
    a-=1
    b-=1
    query.append((a,b,c,d))
mx = 0
for comb in itertools.combinations_with_replacement([i for i in range(1,M+1)],N):
    score = 0
    for a,b,c,d in query:
        if (comb[b] - comb[a] == c):
            score += d
    mx = max(score,mx)
print (mx)
# Original solution
import itertools
N, M, Q = list(map(int, input().split()))
m = list()
res = -100
for i in range(Q):
    n = list(map(int, input().split()))
    m.append(n)
l = list()
for i in range(M):
    l.append(i + 1)
p = itertools.combinations_with_replacement(l, N)
for p in p:
    total = 0
    for i in range(Q):
        num = 0
        a = m[i][0]
        b = m[i][1]
        A = p[a - 1]
        B = p[b - 1]
        C = B - A
        if m[i][2] == C:
            num = num + m[i][3]
            total = total + num
    if res < total:
        res = total
print(res)


# Optimized solution
import itertools
N, M, Q = list(map(int, input().split()))
m = list()
res = -100
for i in range(Q):
    n = list(map(int, input().split()))
    m.append(n)
l = [x for x in range(1, M + 1)]
p = itertools.combinations_with_replacement(l, N)
for p in p:
    total = 0
    for i in range(Q):
        num = 0
        a = m[i][0]
        b = m[i][1]
        A = p[a - 1]
        B = p[b - 1]
        C = B - A
        if m[i][2] == C:
            num = num + m[i][3]
            total = total + num
    if res < total:
        res = total
print(res)
# Original solution
import itertools
N, M, Q = list(map(int, input().split()))
m = list()
res = -100
for i in range(Q):
    n = list(map(int, input().split()))
    m.append(n)
l = [x for x in range(1, M + 1)]
p = itertools.combinations_with_replacement(l, N)
for p in p:
    total = 0
    for i in range(Q):
        num = 0
        a = m[i][0]
        b = m[i][1]
        A = p[a - 1]
        B = p[b - 1]
        C = B - A
        if m[i][2] == C:
            num = num + m[i][3]
            total = total + num
    if res < total:
        res = total
print(res)


# Optimized solution
import itertools
N, M, Q = list(map(int, input().split()))
m = list()
res = -100
for i in range(Q):
    n = list(map(int, input().split()))
    m.append(n)
l = [x for x in range(1, M + 1)]
p = itertools.combinations_with_replacement(l, N)
for p in p:
    total = 0
    for i in range(Q):
        a = m[i][0]
        b = m[i][1]
        A = p[a - 1]
        B = p[b - 1]
        C = B - A
        if m[i][2] == C:
            total = total + m[i][3]
    if res < total:
        res = total
print(res)
# Original solution
from itertools import combinations_with_replacement

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

for x in combinations_with_replacement(list(range(M)), N):
    temp = 0
    for i in range(Q):
        if x[ABCD[i][1]-1]+1 == x[ABCD[i][0]-1]+1 + ABCD[i][2]:
            temp += ABCD[i][3]
    if temp > ans:
        ans = temp
print(ans)

# Optimized solution
from itertools import combinations_with_replacement

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

for A in combinations_with_replacement(list(range(M)), N):
    temp = 0
    for abcd in ABCD:
        if A[abcd[1]-1]+1 == A[abcd[0]-1]+1 + abcd[2]:
            temp += abcd[3]
    if temp > ans:
        ans = temp
print(ans)
# Original solution
from itertools import combinations_with_replacement

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

for A in combinations_with_replacement(list(range(M)), N):
    temp = 0
    for abcd in ABCD:
        if A[abcd[1]-1]+1 == A[abcd[0]-1]+1 + abcd[2]:
            temp += abcd[3]
    if temp > ans:
        ans = temp
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 i in range(Q)]
    ans = 0

    for A in combinations_with_replacement(list(range(M)), N):
        temp = 0
        for abcd in ABCD:
            if A[abcd[1]-1]+1 == A[abcd[0]-1]+1 + abcd[2]:
                temp += abcd[3]
        if temp > ans:
            ans = temp
    print(ans)

if __name__ == '__main__':
    main()
# Original solution
n, m, q = list(map(int, input().split()))
A, B, C, D = [], [], [], []
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    A.append(a - 1)
    B.append(b - 1)
    C.append(c)
    D.append(d)

L = []

def f(l):
    if len(l) == n:
        L.append(l)
        return
    for i in range(l[-1], m + 1):
        f(l + [i])

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

ans = 0

for l in L:
    ans = max(ans, sum(d for a, b, c, d in zip(A, B, C, D) if l[b] - l[a] == c))

print(ans)


# Optimized solution
n, m, q = list(map(int, input().split()))
A, B, C, D = [], [], [], []
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    A.append(a - 1)
    B.append(b - 1)
    C.append(c)
    D.append(d)

L = []

def f(l):
    if len(l) == n:
        L.append(l)
        return
    for i in range(l[-1], m + 1):
        f(l + [i])

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

ans = 0

for l in L:
    ans = max(ans, sum(d for a, b, c, d in zip(A, B, C, D) if l[b] - l[a] == c))

print(ans)
# Original solution
n, m, q = list(map(int, input().split()))
A, B, C, D = [], [], [], []
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    A.append(a - 1)
    B.append(b - 1)
    C.append(c)
    D.append(d)

L = []

def f(l):
    if len(l) == n:
        L.append(l)
        return
    for i in range(l[-1], m + 1):
        f(l + [i])

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

ans = 0

for l in L:
    ans = max(ans, sum(d for a, b, c, d in zip(A, B, C, D) if l[b] - l[a] == c))

print(ans)


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

L = []
def f(l):
    if len(l) == n + 1:
        L.append(l[1:])
        return
    for i in range(l[-1], m + 1):
        f(l + [i])
f([1])

print((max(sum(d for a, b, c, d in T if l[b] - l[a] == c) for l in L)))
# Original solution
def dfs(A):
    if len(A) == n+1:
        p = 0
        for i in range(q):
            if A[b[i]]-A[a[i]] == c[i]:
                p += d[i]
        global ans
        if p > ans:
            ans = p
        return
    num = A[-1]
    global m
    while num <= m:
        dfs(A[:]+[num])
        num += 1
                    

if __name__ == "__main__":
    n, m, q = list(map(int, input().split()))
    a, b, c, d = [None]*q, [None]*q, [None]*q, [None]*q
    for i in range(q):
        a[i], b[i], c[i], d[i] = list(map(int, input().split()))
    ans = 0
    dfs([1])
    print(ans)

# Optimized solution
def dfs(A):
    if len(A) == n+1:
        p = 0
        global ans
        for i in range(q):
            if A[b[i]]-A[a[i]] == c[i]:
                p += d[i]
        if p > ans:
            ans = p
        return
    num = A[-1]
    while num <= m:
        dfs(A[:]+[num])
        num += 1


if __name__ == "__main__":
    n, m, q = list(map(int, input().split()))
    a, b, c, d = [None]*q, [None]*q, [None]*q, [None]*q
    for i in range(q):
        a[i], b[i], c[i], d[i] = list(map(int, input().split()))
    ans = 0
    dfs([1])
    print(ans)
# Original solution
N, M, Q = [int(n) for n in input().split()]
a, b, c, d = [list(x) for x in zip(*[[int(n) for n in input().split()] for _ in range(Q)])]
ans = 0
def dfs(A):
    global ans
    if len(A) == N+1:
        now = 0
        for i in range(Q):
            if A[b[i]] - A[a[i]] == c[i]:
                now += d[i]
        ans = max(ans, now)
        return
    tail = A[-1] if len(A) > 0 else 1
    for v in range(tail, M+1):
        A.append(v)
        dfs(A)
        A.pop()

dfs([])
print(ans)

# Optimized solution
N, M, Q = [int(n) for n in input().split()]
a, b, c, d = [list(x) for x in zip(*[[int(n) for n in input().split()] for _ in range(Q)])]
ans = 0

def dfs(A):
    global ans
    if len(A) == N:
        now = 0
        for i in range(Q):
            if A[b[i]-1] - A[a[i]-1] == c[i]: now += d[i]
        ans = max(ans, now)
        return

    for v in range(A[-1], M+1):
        A.append(v)
        dfs(A)
        A.pop()

dfs([1])
print(ans)
# Original solution
# import numpy as np
# import queue
# import heapq
from itertools import combinations_with_replacement


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

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

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

    max_total = 0
    for A in As:
        total = 0

        for i, q in enumerate(queries):
            if A[q[1]-1] - A[q[0]-1] == q[2]:
                total += q[3]

        max_total = max(max_total, total)

    print(max_total)


if __name__ == "__main__":
    main()


# Optimized solution
from itertools import combinations_with_replacement


def main():
    n_ints, max_int, n_queries = list(map(int, input().split()))

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

    possible_sequences = combinations_with_replacement(list(range(1, max_int+1)), n_ints)

    max_score = 0
    for sequence in possible_sequences:
        score = 0

        for i, q in enumerate(queries):
            a, b, c, d = q
            if sequence[b-1] - sequence[a-1] == c:
                score += d

        max_score = max(max_score, score)

    print(max_score)


if __name__ == "__main__":
    main()
# Original solution
import itertools
n,m,q=list(map(int,input().split()))
Q=[]
for i in range(q):
    a,b,c,d=list(map(int,input().split()))
    Q.append([a-1,b-1,c,d])    
ans=0
for i in range(m):
    L=[i for i in range(1,n+i-1)]
    C=itertools.combinations(L,n-2)
    for c in C:
        #print(c)
        cnt=0
        A=[1]
        now=0
        for j in range(n-2):
            A.append(A[-1]+c[j]-now-1)
            now=c[j]
        A.append(i+1)
        #print(A)
        for que in Q:
            if A[que[1]]-A[que[0]]==que[2]:
                cnt+=que[3]
        ans=max(cnt,ans)
print(ans)

# Optimized solution
import copy
n,m,q=list(map(int,input().split()))
Q=[]
for i in range(q):
    a,b,c,d=list(map(int,input().split()))
    Q.append([a-1,b-1,c,d])    
B=[]
def dfs(A,l):
    if l==n:
        B.append(A)
    else:
        for i in range(A[-1],m+1):
            nA=copy.copy(A)
            nA.append(i)
            dfs(nA,l+1)
dfs([1],1)

ans=0
for ls in B:
    cnt=0
    for que in Q:
        if ls[que[1]]-ls[que[0]]==que[2]:
            cnt+=que[3]
    ans=max(cnt,ans)
print(ans)