CodeEff/ECCO huggingface dataset:
# Original solution
from itertools import combinations_with_replacement
# n個の数列、1~mの中で数列を作る
n, m, q = list(map(int, input().split()))

comb = combinations_with_replacement(list(range(1, m + 1)), n)
abcd = [list(map(int, input().split())) for _ in [0] * q]

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

print(ans)


# Optimized solution
from itertools import combinations_with_replacement
# n個の数列、1~mの中で数列を作る
# 2<=n<=10
# 1<=m<=10
n, m, q = list(map(int, input().split()))

comb = combinations_with_replacement(list(range(1, m + 1)), n)
abcd = [list(map(int, input().split())) for _ in [0] * q]

ans = 0
for A in comb:
    cnt = 0
    for a,b,c,d in abcd:
        if A[b - 1] - A[a - 1] == c:
            cnt += d
    ans = max(ans, cnt)

print(ans)
# Original solution
N, M, Q = list(map(int, input().strip().split()))
pattern_list = []
for _ in range(Q):
	pattern = tuple(map(int, input().strip().split()))
	pattern_list.append(pattern)

def create(N,M):
	S = [[] for _ in range(N+1)]
	for m in range(1,M+1):
		S[1].append([m])
	for n in range(1,N):
		for s in S[n]:
			Max = s[-1]
			for i in range(Max, M+1):
				S[n+1].append(s+[i])
	return S

highest = 0
for letters in create(N,M)[N]:
	point = 0
	for a,b,c,d in pattern_list:
		if int(c) == letters[int(b-1)]-letters[int(a-1)]:
			point += d
	highest = max([highest, point])

print(highest)

# Optimized solution
N, M, Q = list(map(int, input().strip().split()))
pattern_list = []
for _ in range(Q):
    pattern = tuple(map(int, input().strip().split()))
    pattern_list.append(pattern)

def create(N,M):
    S = [[] for _ in range(N+1)]
    for m in range(1,M+1):
        S[1].append([m])
    for n in range(1,N):
        for s in S[n]:
            Max = s[-1]
            for i in range(Max, M+1):
                S[n+1].append(s+[i])
    return S

highest = 0
for letters in create(N,M)[N]:
    point = 0
    for a,b,c,d in pattern_list:
        if c == letters[b-1]-letters[a-1]:
            point += d
    highest = max([highest, point])

print(highest)
# Original solution
import math
#X=int(input())
N,M,Q=list(map(int,input().split()))
S=[[]for i in range(Q)]
for i in range(Q):
    a=list(map(int,input().split()))
    S[i]=a
ans=0
T=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):
                                        D=[a,b,c,d,e,f,g,h,i,j]
                                        for k in range(Q):
                                            if D[S[k][1]-1]-D[S[k][0]-1]==S[k][2]:
                                                ans+=S[k][3]
                                        if ans>=T:
                                            T=ans
                                        ans = 0
print(T)


# Optimized solution
import math
import sys
sys.setrecursionlimit(10**7)
#X=int(input())
N,M,Q=list(map(int,input().split()))
S=[[]for i in range(Q)]
for i in range(Q):
    a=list(map(int,input().split()))
    S[i]=a
ans=0
T=0
def solve(l):
    global ans
    if len(l)>=N:
        tmp=0
        for k in range(Q):
            if l[S[k][1]-1]-l[S[k][0]-1]==S[k][2]:
                tmp+=S[k][3]
        ans=max(ans,tmp)
        return
    for i in range(l[-1],M+1):
        solve(l+[i])
solve([1])
print(ans)
# Original solution
import itertools
n, m, q = list(map(int, input().split()))
lst = [list(map(int, input().split())) for i in range(q)]
ans = 0

for A in itertools.combinations_with_replacement(list(range(1, m + 1)), n):
    flag = 0
    for k in range(n - 1):
        if A[k + 1] < A[k]:
            flag = 1
    if flag == 0:
        point = 0
        for l in range(q):
            if A[lst[l][1] - 1] - A[lst[l][0] - 1] == lst[l][2]:
                point += lst[l][3]
        ans = max(ans, point)

print(ans)


# Optimized solution
import sys
sys.setrecursionlimit(1000000)
n, m, q = list(map(int, input().split()))
lst = [list(map(int, input().split())) for i in range(q)]
ans = 0

def dfs(A):
    global ans
    if len(A) == n + 1:
        point = 0
        for i in range(q):
            if A[lst[i][1]] - A[lst[i][0]] == lst[i][2]:
                point += lst[i][3]
                ans = max(ans, point)
        return
    A.append(A[-1])
    while A[-1] <= m:
        dfs(A[:])
        A[-1] += 1

dfs([1])

print(ans)
# Original solution
from itertools import combinations_with_replacement
n,m,q = list(map(int, input().split()))
abcd = []
for _ in range(q):
  a,b,c,d = list(map(int, input().split()))
  abcd.append([a,b,c,d])

nums = list(range(1,m+1))
l = list(combinations_with_replacement(nums, n))

ans = 0
for i in l:
  p = 0
  for j in abcd:
    b = i[j[1]-1]
    a = i[j[0]-1]
    if b - a == j[2]:
      p += j[3]
  ans = max(ans, p)
  
print(ans)

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

nums = list(range(1,m+1))
l = list(combinations_with_replacement(nums, n))

ans = 0
for i in l:
  p = 0
  for j in abcd:
    if i[j[1]-1] - i[j[0]-1] == j[2]:
      p += j[3]
  ans = max(ans, p)
  
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 _ in range(q)]

nums = list(combinations_with_replacement(list(range(1,m+1)), n))

ans = 0
for i in nums:
  t = 0
  for j in abcd:
    s = i[j[1]-1] - i[j[0]-1]
    if s == j[2]:
      t += j[3]
  ans = max(ans, t)
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 _ in range(q)]

nums = list(range(1, m+1))
ans = 0
for i in combinations_with_replacement(nums, n):
  t = 0
  for j in abcd:
    if i[j[1]-1] - i[j[0]-1] == j[2]:
      t += j[3]
  ans = max(ans, t)
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 _ in range(q)]

num = list(combinations_with_replacement(list(range(1,m+1)), n))

ans = 0
for i in num:
  t = 0
  for j in abcd:
    if i[j[1]-1] - i[j[0]-1] == j[2]:
      t += j[3]
  ans = max(ans, t)
  
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 _ in range(q)]

ans = 0
for i in combinations_with_replacement(list(range(1,m+1)),n):
  t = 0
  for j in abcd:
    if i[j[1]-1] - i[j[0]-1] == j[2]:
      t += j[3]
  ans = max(ans, t)
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 _ in range(q)]

nums = combinations_with_replacement(list(range(1,m+1)), n)
ans = 0
for a in nums:
  t = 0
  for i in abcd:
    if a[i[1]-1] - a[i[0]-1] == i[2]: t += i[3]
  if ans < t: ans = t
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 _ in range(q)]

ans = 0
for a in combinations_with_replacement(list(range(1,m+1)), n):
  t = 0
  for i in abcd:
    if a[i[1]-1] - a[i[0]-1] == i[2]: t += i[3]
  if ans < t: ans = t
print(ans)
# Original solution
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

def check(num):
    res = 0
    for a,b,c,d in abcd:
        if num[b-1] - num[a-1] == c:
            res += d
    return res

def dfs(x, y, z, res):
    if y == n:
        res = max(res, check(x))
        return res
    for i in range(z, m+1):
        res = max(res, dfs(x + [i], y+1, i, res))
    return res

n, m, q = LI()
abcd = [LI() for _ in range(q)]
ans = dfs([1], 0, 1, 0)
print(ans)
        

    

# Optimized solution
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

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

n, m, q = LI()
abcd = [LI() for _ in range(q)]
ans = dfs([1])
print(ans)
# Original solution
import itertools
n,m,q = list(map(int,input().split()))
num_ls = [[0,0,0,0] for _ in range(q)]
for i in range(q):
    num_ls[i] = list(map(int,input().split()))

a_base = [i for i in range(1,m+1)]
a_ls = []
for A in itertools.combinations_with_replacement(a_base, n):
    istarget = True
    for i in range(n-1):
        if A[i] > A[i+1]:
            istarget = False
    if istarget:
        a_ls.append(list(A))
ans = 0
for A in a_ls:
    value_of_A = 0
    for a,b,c,d in num_ls:
        if A[b-1] - A[a-1] == c:
            value_of_A += d
    ans = max(ans, value_of_A)
print(ans)

# Optimized solution
N,M,Q = list(map(int,input().split()))
req_ls = []
l_ind_ls = [0] * Q
r_ind_ls = [0] * Q
diff_ls = [0] * Q
point_ls = [0] * Q
for i in range(Q):
    l_ind_ls[i], r_ind_ls[i], diff_ls[i], point_ls[i] = list(map(int, input().split()))
    l_ind_ls[i] -= 1
    r_ind_ls[i] -= 1

def score(ls):
    tmp = 0
    for l_ind, r_ind, diff, point in zip(l_ind_ls,r_ind_ls,diff_ls,point_ls):
        if ls[r_ind] - ls[l_ind] == diff:
            tmp += point
    return tmp

def dfs(ls):
    if len(ls) == N:
        return score(ls)
    res = 0
    last = ls[-1] if len(ls) > 0 else 1
    for nex in range(last, M+1):
        ls.append(nex)
        res = max(res, dfs(ls))
        ls.pop()
    return res
print((dfs([])))
# Original solution
N,M,Q = list(map(int,input().split()))
req_ls = []
l_ind_ls = [0] * Q
r_ind_ls = [0] * Q
diff_ls = [0] * Q
point_ls = [0] * Q
for i in range(Q):
    l_ind_ls[i], r_ind_ls[i], diff_ls[i], point_ls[i] = list(map(int, input().split()))
    l_ind_ls[i] -= 1
    r_ind_ls[i] -= 1

def score(ls):
    tmp = 0
    for l_ind, r_ind, diff, point in zip(l_ind_ls,r_ind_ls,diff_ls,point_ls):
        if ls[r_ind] - ls[l_ind] == diff:
            tmp += point
    return tmp

def dfs(ls):
    if len(ls) == N:
        return score(ls)
    res = 0
    last = ls[-1] if len(ls) > 0 else 1
    for nex in range(last, M+1):
        ls.append(nex)
        res = max(res, dfs(ls))
        ls.pop()
    return res
print((dfs([])))




# Optimized solution
length, Max, q = list(map(int,input().split()))
query = [[0]*4 for _ in range(q)]
for i in range(q):
    a,b,c,d = list(map(int,input().split()))
    query[i] = [a-1,b-1,c,d]
ans = 0
def dfs(A):
    if len(A) == length:
        global ans
        score = 0
        for i in range(q):
            a,b,c,d = query[i]
            if A[b] - A[a] == c:
                score += d
        ans = max(ans, score)
        return
    last = A[-1]
    for i in range(last,Max+1):
        A.append(i)
        dfs(A)
        A.pop()
dfs([1])
print(ans)
# Original solution
import itertools
n,m,q=list(map(int,input().split()))
Se=[]
for i in range(q):
    Se.append(list(map(int,input().split())))
#print(Se)
integerset={i for i in range(1,m+1)}
#print(integerset)
everyset=itertools.combinations_with_replacement(integerset,n)
max=0
for v in everyset:
  #  print(v)
 #   print(len(v))
    sum=0
    for i in range(len(Se)):
        if v[Se[i][1]-1]-v[Se[i][0]-1]==Se[i][2]:
            sum+=Se[i][3]
   # print(sum)
    if sum>max:
        max=sum
print(max)

    

# Optimized solution
import itertools
n,m,q=list(map(int,input().split()))
Se=[]
for i in range(q):
    Se.append(tuple(map(int,input().split())))
#print(Se)
integerset={i for i in range(1,m+1)}
#print(integerset)
everyset=itertools.combinations_with_replacement(integerset,n)
max=0
for v in everyset:
  #  print(v)
 #   print(len(v))
    sum=0
    for i in range(len(Se)):
        if v[Se[i][1]-1]-v[Se[i][0]-1]==Se[i][2]:
            sum+=Se[i][3]
   # print(sum)
    if sum>max:
        max=sum
print(max)
# Original solution
import itertools
n,m,q=list(map(int,input().split()))
Se=[]
for i in range(q):
    Se.append(tuple(map(int,input().split())))
#print(Se)
integerset={i for i in range(1,m+1)}
#print(integerset)
everyset=itertools.combinations_with_replacement(integerset,n)
max=0
for v in everyset:
  #  print(v)
 #   print(len(v))
    sum=0
    for i in range(len(Se)):
        if v[Se[i][1]-1]-v[Se[i][0]-1]==Se[i][2]:
            sum+=Se[i][3]
   # print(sum)
    if sum>max:
        max=sum
print(max)


# Optimized solution
import itertools
def main():
    n,m,q=list(map(int,input().split()))
    Se=[]
    for i in range(q):
        Se.append(tuple(map(int,input().split())))
    #print(Se)
    integerset={i for i in range(1,m+1)}
    #print(integerset)
    everyset=itertools.combinations_with_replacement(integerset,n)
    max=0
    for v in everyset:
    #  print(v)
    #   print(len(v))
        sum=0
        for i in range(len(Se)):
            if v[Se[i][1]-1]-v[Se[i][0]-1]==Se[i][2]:
                sum+=Se[i][3]
    # print(sum)
        if sum>max:
            max=sum
    print(max)
if __name__=="__main__":
    main()
# Original solution
#n=int(input())
n,m,q=list(map(int,input().split()))
#l=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

al=[]
bl=[]
cl=[]
dl=[]
l=[]
ansl=[0 for i in range(n+1)]
for i in range(q):
    a,b,c,d=list(map(int,input().split()))
    al.append(a)
    bl.append(b)
    cl.append(c)
    dl.append(d)
    l.append([a,b,c,d])

def score(seq):
    temp=0
    for a,b,c,d in zip(al,bl,cl,dl):
        if seq[b-1]-seq[a-1]==c:
            temp+=d
    return temp

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


# Optimized solution
#n=int(input())
n,m,q=list(map(int,input().split()))
#hl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

al=[0]*q
bl=[0]*q
cl=[0]*q
dl=[0]*q

for i in range(q):
    al[i],bl[i],cl[i],dl[i]=list(map(int,input().split()))

def score_calc(seq):
    res=0
    for a,b,c,d in zip(al,bl,cl,dl):
        if seq[b-1]-seq[a-1]==c:
            res+=d
    return res
score_list=[]
def dfs(seq):
    if len(seq)==n:
        score_list.append(score_calc(seq))
        return 
    lastone=seq[-1] if len(seq)>0 else 1
    for v in range(lastone,m+1):
        seq.append(v)
        dfs(seq)
        seq.pop()

dfs([])
print((max(score_list)))
# Original solution
n, m, q = list(map(int, input().split()))
plist = []
for _ in range(q):
    plist.append(list(map(int, input().split())))

alist = []


def bfs(depth: int, l: list):
    global n, m, alist
    if depth == n:
        alist.append(l[1:])
        return
    c = l[depth]
    cl = l.copy()
    cl.append(c)
    bfs(depth+1, cl.copy())
    while c < m:
        c += 1
        cl = l.copy()
        cl.append(c)
        bfs(depth + 1, cl.copy())


bfs(0, [1])
pmax = 0
for a in alist:
    point = 0
    for p in plist:
        if (a[p[1]-1] - a[p[0]-1] == p[2]):
            point += p[3]
    pmax = max(pmax, point)

print(pmax)


# Optimized solution
n, m, q = list(map(int, input().split()))
plist = []
for _ in range(q):
    plist.append(list(map(int, input().split())))

alist = []


def bfs(depth: int, l: list):
    global n, m, alist
    if depth == n:
        alist.append(l[1:])
        return
    c = l[depth]
    cl = l.copy()
    cl.append(c)
    bfs(depth+1, cl)
    while c < m:
        c += 1
        cl = l.copy()
        cl.append(c)
        bfs(depth + 1, cl)


bfs(0, [1])
pmax = 0
for a in alist:
    point = 0
    for p in plist:
        if (a[p[1]-1] - a[p[0]-1] == p[2]):
            point += p[3]
    pmax = max(pmax, point)

print(pmax)
# Original solution
import itertools
n,m,q=list(map(int,input().split()))
lis=[]
ans=0

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

for i in itertools.combinations_with_replacement(list(range(1,m+1)),n):
    tmp=0
    for j in range(q):
        if i[lis[j][1]] - i[lis[j][0]] == lis[j][2]:
            tmp+=lis[j][3]
    ans=max(ans,tmp)
print(ans)

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

for i in itertools.combinations_with_replacement(list(range(1,m+1)),n):
    tmp=0
    for j in range(q):
        if i[b[j]] - i[a[j]] == c[j]:
            tmp+=d[j]
    ans=max(ans,tmp)
print(ans)
# Original solution
#!/usr/bin/env python3

def main():
    import sys

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

    def dfs(A: list):
        nonlocal ans
        if len(A) == N:
            ans = max(ans, calc(A))
        elif len(A) == 0:
            for nx in range(1, M + 1):
                dfs(A + [nx])
        else:
            for nx in range(A[-1], M + 1):
                dfs(A + [nx])

    input = sys.stdin.readline

    N, M, Q = list(map(int, input().split()))
    lst = [list(map(int, input().split())) for _ in range(Q)]
    ans = 0
    dfs([])
    print(ans)


main()


# Optimized solution
#!/usr/bin/env python3
def main():
    import sys
    from itertools import combinations_with_replacement

    input = sys.stdin.readline

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

    ans = 0
    for pattern in combinations_with_replacement(list(range(1, M + 1)), N):
        res = 0
        pattern = list(pattern)
        for a, b, c, d in lst:
            if pattern[b - 1] - pattern[a - 1] == c:
                res += d
        ans = max(ans, res)

    print(ans)


main()
# Original solution
from itertools import combinations_with_replacement

n, m, q = list(map(int, input().split()))
abcd = [[int(i) for i in input().split()] for _ in range(q)]
s = 0
for p in combinations_with_replacement(list(range(1, m + 1)), n):
    t = 0
    for a, b, c, d in abcd:
        t += d * (p[b - 1] - p[a - 1] == c)
    s = max(s, t)
print(s)

# Optimized solution
from itertools import combinations_with_replacement

def main():
    n, m, q = list(map(int, input().split()))
    abcd = [[int(i) for i in input().split()] for _ in range(q)]
    s = 0
    for p in combinations_with_replacement(list(range(1, m + 1)), n):
        t = 0
        for a, b, c, d in abcd:
            t += d * (p[b - 1] - p[a - 1] == c)
        s = max(s, t)
    print(s)

if __name__ == '__main__':
    main()
# Original solution
import sys
sys.setrecursionlimit(1000000000)
input = sys.stdin.readline
n,m,q=list(map(int,input().split()))
x=[list(map(int,input().split())) for i in range(q)]
for i in range(q):
    for j in range(2):
        x[i][j]-=1
a=[]
def dfs(v):
    if len(v)>n:
        return
    if len(v)==n:
        a.append(v)
    for i in range(int(v[-1]),m+1):
        dfs(v+[i])
for i in range(1,m+1):
    dfs([i])
ans=0
for i in a:
    now=0
    for k in range(q):
        if i[x[k][1]]-i[x[k][0]]==x[k][2]:
            now+=x[k][3]
    ans=max(now,ans)
print(ans)

# Optimized solution
from itertools import combinations_with_replacement
n,m,q=list(map(int,input().split()))
a,b,c,d=[0]*q,[0]*q,[0]*q,[0]*q
for i in range(q):
    x,y,z,w=list(map(int,input().split()))
    x-=1
    y-=1
    a[i],b[i],c[i],d[i]=x,y,z,w
ans=0
for v in combinations_with_replacement(list(range(1,m+1)),n):
    res=0
    for i in range(q):
        if v[b[i]]-v[a[i]]==c[i]:
            res+=d[i]
    ans=max(ans,res)
print(ans)
# Original solution
# C - Many Requirements

n, m, q = list(map(int, input().split()))
lst = []
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    lst.append((a, b, c, d))  # 得点を先頭に

def allseq(n, m):
    if n == 0:
        yield []
    else:
        for rest in allseq(n - 1, m):
            for i in range(rest[-1] if len(rest) > 0 else 1, m+1):
                yield rest + [i]

x = 0
for seq in allseq(n, m):
    s = 0
    for a, b, c, d in lst:
        if seq[b-1] - seq[a-1] == c:
            s += d
    x = max(x, s)

print(x)


# Optimized solution
# C - Many Requirements

from itertools import combinations_with_replacement

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

x = max(sum(d for a, b, c, d in lst if seq[b-1] - seq[a-1] == c)
        for seq in combinations_with_replacement(list(range(1, m+1)), n))
print(x)
# Original solution
import itertools
n,m,q=list(map(int,input().split()))
abcd=[list(map(int,input().split()))for _ in range(q)]
ans=0
for i in itertools.combinations_with_replacement(list(range(m)),n):
  anss=0
  for a,b,c,d in abcd:
    if i[b-1]-i[a-1]==c:anss+=d
  ans=max(ans,anss)
print(ans)

# Optimized solution
import itertools
n,m,q=list(map(int,input().split()))
abcd=[list(map(int,input().split()))for _ in range(q)]
print((max(sum(d for a,b,c,d in abcd if i[b-1]-i[a-1]==c)for i in itertools.combinations_with_replacement(list(range(m)),n))))
# Original solution
import itertools;n,m,q,*l=list(map(int,open(0).read().split()));print((max(sum(d for a,b,c,d in[l[j*4:j*4+4]for j in range(q)]if i[b-1]-i[a-1]==c)for i in itertools.combinations_with_replacement(list(range(m)),n))))

# Optimized solution
n,m,q=list(map(int,input().split()))
abcd=[list(map(int,input().split()))for _ in range(q)]
ans=0
import itertools
for i in itertools.combinations_with_replacement(list(range(m)),n):
  anss=0
  for a,b,c,d in abcd:
    if i[b-1]-i[a-1]==c:anss+=d
  ans=max(ans,anss)
print(ans)
# Original solution
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[i] -=1
    b[i] -=1
def score(A):
    tmp=0
    for ai,bi,ci,di in zip(a,b,c,d):
        if A[bi]-A[ai] == ci:
            tmp+=di
    return tmp
def dfs(A):
    if len(A) == N:
        return score(A)
    res = 0
    last = A[-1] if len(A)>0 else 0
    for i in range(last,M):
        A.append(i)
        res=max(res,dfs(A))
        A.pop()
    return res
print((dfs([])))      

# Optimized solution
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[i]-=1
    b[i]-=1
def score(A):
    tmp=0
    for ai,bi,ci,di in zip(a,b,c,d):
        if A[bi]-A[ai] == ci:
            tmp+=di
    return tmp


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

print((dfs([])))