CodeEff/ECCO huggingface dataset:
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import deque
ans = 0
def main():
    N, M, Q = list(map(int, readline().split()))
    query = []
    for _ in range(Q):
        a, b, c, d = list(map(int, readline().split()))
        query.append((a, b, c, d))
    

    def getScore(A):
        res = 0
        for a, b, c, d in query:
            if A[b-1]-A[a-1] == c:
                res += d
        return res

    A = deque()
    def dfs():
        global ans
        n = len(A)
        if n == N:
            ans = max(getScore(A), ans)
        else:
            if n == 0:
                k = 1
            else:
                k = A[-1]
            for i in range(k, M+1):
                A.append(i)
                dfs()
                A.pop()
    dfs()
    print(ans)          
if __name__ == '__main__':
    main()

# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(1000000) 
ans = 0
def main():
    N, M, Q = list(map(int, readline().split()))
    query = []
    for _ in range(Q):
        a, b, c, d = list(map(int, readline().split()))
        a -= 1
        b -= 1
        query.append((a, b, c, d))

    def slv(A):    
        s = 0
        for a, b, c, d in query:
            if A[b]-A[a] == c:
                s += d
        return s
    #数列Aを構築
    #数列Aを構築できたら計算処理
    from collections import deque 
    A = deque()
    def dfs(p):
        global ans
        if p == N:
            s = 0
            for a, b, c, d in query:
                if A[b]-A[a] == c:
                    s += d
            ans = max(s, ans)
            return
        if len(A) == 0:
            k = 1
        else:
            k = A[p-1]
        for i in range(k, M+1):
            A.append(i)
            dfs(p+1)
            A.pop()
    dfs(0)
    print(ans)
if __name__ == '__main__':
    main()
# Original solution
N, M, Q = list(map(int, input().split()))
ans = 0
zenbu = []
def tukuru(cur, last):
    if len(cur) == N:
        zenbu.append(cur)
        return
    for j in range(last, M+1):
        tukuru(cur+[j], j)
tukuru([], 1)
D = [0]*len(zenbu)
for i in range(Q):
    a, b, c, d = list(map(int, input().split()))
    for i, p in enumerate(zenbu):
        if p[b-1] - p[a-1] == c:
            D[i] += d
print((max(D)))

# Optimized solution
from itertools import combinations_with_replacement
N, M, Q = list(map(int, input().split()))
a = list()
b = list()
c = list()
d = list()

for _ 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)
ans = 0

for A in combinations_with_replacement(list(range(1, M+1)), r=N):
    dmax = 0
    for q in range(Q):
        if A[b[q]] - A[a[q]] == c[q]:
            dmax += d[q]
    ans = max(ans, dmax)
print(ans)
# Original solution
N,M,Q=list(map(int, input().split()))

a_ls=[]
b_ls=[]
c_ls=[]
d_ls=[]

for vnwrjvnwakn in range(Q):
	abcd=list(map(int, input().split()))
	a_ls+=[abcd[0]]
	b_ls+=[abcd[1]]
	c_ls+=[abcd[2]]
	d_ls+=[abcd[3]]



import itertools
As=itertools.combinations_with_replacement(list(range(1,M+1)),N)
ans=0
for A in As:
	score=0
	for i in range(Q):
		if list(A)[b_ls[i]-1]-list(A)[a_ls[i]-1]==c_ls[i]:
			score+=d_ls[i]
	
	ans=max(ans,score)


print(ans)


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

a_ls=[]
b_ls=[]
c_ls=[]
d_ls=[]

for vnwrjvnwakn in range(Q):
	abcd=list(map(int, input().split()))
	a_ls+=[abcd[0]]
	b_ls+=[abcd[1]]
	c_ls+=[abcd[2]]
	d_ls+=[abcd[3]]



import itertools
As=itertools.combinations_with_replacement(list(range(1,M+1)),N)
ans=0
for A in As:
	score=0
	Als=list(A)
	for i in range(Q):
		if Als[b_ls[i]-1]-Als[a_ls[i]-1]==c_ls[i]:
			score+=d_ls[i]
	
	ans=max(ans,score)


print(ans)
# Original solution
n, m, q = list(map(int, input().split()))
abcd = [tuple(map(int, input().split()))for _ in range(q)]
ls = []


def dfs(x, s):
    if len(s) == n:
        ls.append(s)
        return 0
    else:
        for y in range(x, m):
            dfs(y, s+str(y))


dfs(0, "")
ans = 0
for x in ls:
    x = tuple([int(x)+1 for x in list(x)])
    cnt = 0
    for a, b, c, d in abcd:
        a -= 1
        b -= 1
        if x[b]-x[a] == c:
            cnt += d
    if ans < cnt:
        ans = cnt
print(ans)

# Optimized solution
from itertools import combinations_with_replacement
n, m, q = list(map(int, input().split()))
abcd = [tuple(map(int, input().split()))for _ in range(q)]
ans = 0
for x in combinations_with_replacement(list(range(1, m+1)), n):
    cnt = 0
    for a, b, c, d in abcd:
        if x[b-1]-x[a-1] == c:
            cnt += d
    if ans < cnt:
        ans = cnt

print(ans)
# Original solution
def resolve():
    import itertools
    n, m, q = list(map(int, input().split()))
    cond = [list(map(int, input().split())) for _ in range(q)]
    ans = 0
    for i in list(itertools.combinations_with_replacement(list(range(1, m+1)), n)):
        point = 0
        for j in range(q):
            if i[cond[j][1]-1] - i[cond[j][0]-1] == cond[j][2]:
                point += cond[j][3]
        ans = max(ans, point)
    print(ans)
resolve()

# Optimized solution
def resolve():
    import itertools
    n, m, q = list(map(int, input().split()))
    cond = [list(map(int, input().split())) for _ in range(q)]
    ans = 0
    for i in itertools.combinations_with_replacement(list(range(1, m+1)), n):
        point = 0
        for j in range(q):
            if i[cond[j][1]-1] - i[cond[j][0]-1] == cond[j][2]:
                point += cond[j][3]
        ans = max(ans, point)
    print(ans)
resolve()
# Original solution
n, m, q = list(map(int, input().split()))
abcd = []
for i in range(q):
    abcd.append(list(map(int, input().split())))

A = []


def dfs(num):
    if len(num) == n:
        A.append(num)
    else:
        now = num[-1]
        for i in range(now, m + 1):
            dfs(num + [i])


for i in range(1, 10):
    dfs([i])

ans = 0
for i in range(len(A)):
    now = A[i]
    point = 0
    for j in range(q):
        a, b, c, d = abcd[j]
        a -= 1
        b -= 1
        if int(now[b]) - int(now[a]) == c:
            point += d

    ans = max(ans, point)

print(ans)


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

A = []


def dfs(num):
    if len(num) == n:
        A.append(num)
    else:
        now = num[-1]
        for i in range(now, m + 1):
            dfs(num + [i])


for i in range(1, 10):
    dfs([i])

ans = 0
for now in A:
    point = 0
    for a, b, c, d in abcd:
        if now[b-1] - now[a-1] == c:
            point += d

    ans = max(ans, point)

print(ans)
# Original solution
import copy


def dfs(A):
    global ans
    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(copy.copy(A))
        A[-1] += 1


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


# Optimized solution
def dfs(A):
    global ans
    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


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 itertools

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

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

#print(abcd_list)
list_tmp = []
for i in range(1, M + 1):
    list_tmp.append(i)

comb_A = list(itertools.combinations_with_replacement(list_tmp, N))


max_score = 0
for A in comb_A:
    tmp_score = 0
    #print(A)
    for q in range(Q):
        #print(A)
        a = abcd_list[q][0]
        b = abcd_list[q][1]
        c = abcd_list[q][2]
        d = abcd_list[q][3]

        A_a = A[a - 1]
        A_b = A[b - 1]

        if(A_b - A_a == c):
            tmp_score += d
        
    max_score = max(max_score, tmp_score)

print(max_score)

# Optimized solution
import itertools

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

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

#print(abcd_list)
list_tmp = []
for i in range(1, M + 1):
    list_tmp.append(i)

comb_A = list(itertools.combinations_with_replacement(list_tmp, N))


max_score = 0
for A in comb_A:
    tmp_score = 0
    for q in range(Q):
        A_a = A[abcd_list[q][0] - 1]
        A_b = A[abcd_list[q][1] - 1]

        if(A_b - A_a == abcd_list[q][2]):
            tmp_score += abcd_list[q][3]
        
    max_score = max(max_score, tmp_score)

print(max_score)
# Original solution
N, M, Q = list(map(int, input().split()))

A = [list(map(int, input().split())) for _ 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):
                                        tmp = 0
                                        MM = [a, b, c, d, e, f, g, h, i, j][:N]
                                        for ai, bi, ci, di in A:
                                            if MM[bi-1]-MM[ai-1] == ci:
                                                tmp += di
                                        ans = max(ans, tmp)
print(ans)



# Optimized solution
from itertools import combinations_with_replacement

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

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

ans = 0
for i in combinations_with_replacement(list(range(1, M+1)), N):
    tmp = 0
    for a, b, c, d in A:
        if i[b-1] - i[a-1] == c:
            tmp += d
    ans = max(ans, tmp)
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())))

A_N = [[i] for i in range(1, M+1)]
for i in range(N-1):
  a = []
  for j in range(len(A_N)):
    last = A_N[j][-1]
    for k in range(last, M+1):
      a.append(A_N[j] + [k])
  A_N = a

high_score = 0
for i in range(len(A_N)):
    A = A_N[i]
    score = 0
    for j in range(len(l)):
        aj, bj, cj, dj = l[j]
        if A[bj-1]-A[aj-1] == cj:
            score += dj
        high_score = max(high_score, score) 
        
print(high_score)

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

A_N = [[i] for i in range(1, M+1)]
for i in range(N-1):
  a = []
  for j in range(len(A_N)):
    last = A_N[j][-1]
    for k in range(last, M+1):
      a.append(A_N[j] + [k])
  A_N = a

high_score = 0
for i in range(len(A_N)):
  A = A_N[i]
  score = 0
  for j in range(len(l)):
    aj, bj, cj, dj = l[j]
    if A[bj-1]-A[aj-1] == cj:
      score += dj
  if high_score < score:
    high_score = score 

print(high_score)
# Original solution
import copy
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 = [1]
ans = 0
def dfs(lis):
    if len(lis) == n+1:
        now = 0
        for i in range(q):
            if lis[b[i]] - lis[a[i]] == c[i]:
                now += d[i]
        global ans
        ans = max(now, ans)
        return 0
    lis.append(lis[-1])
    while lis[-1] <= m:
        newlis = copy.deepcopy(lis)
        dfs(newlis)
        lis[-1] += 1
dfs(A)
print(ans)

# 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()))
ans = 0
for A in itertools.combinations_with_replacement(list(range(1, m+1)), 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)
print(ans)
# Original solution
import itertools
 
N,M,Q=list(map(int,input().split()))
all=[]
 
for i in range(Q):
  all.append(list(map(int,input().split())))
#print(all)
#print(len(all))
#print(all[0][1])
 
x=sorted([i for i in range(1,M+1)])
maxdsum=0
 
 
for A in itertools.combinations_with_replacement(x, N):
  dsum=0
  #print("A=="+str(A))
  for num in range(Q):
    if A[all[num][1]-1]-A[all[num][0]-1]==all[num][2]:
      dsum+=all[num][3]
  if maxdsum<dsum:
    maxdsum=dsum
print(maxdsum)

# Optimized solution
import itertools
 
N,M,Q=list(map(int,input().split()))
all=[]
for i in range(Q):
  all.append(list(map(int,input().split())))
 
x=[i for i in range(1,M+1)]
maxdsum=0
 
for A in itertools.combinations_with_replacement(x, N):
  dsum=0
  for num in range(Q):
    if A[all[num][1]-1]-A[all[num][0]-1]==all[num][2]:
      dsum+=all[num][3]
  if maxdsum<dsum:
    maxdsum=dsum
print(maxdsum)
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

from collections import deque

def dfs(min_val, length):
    if length == 0:
        return list([deque()])
    if min_val == m:
        return list([deque([m]) * length])
    ans = list()
    for i in range(min_val, m+1):
        remain = dfs(i, length-1)
        for j in remain:
            j.appendleft(i)
        ans.extend(remain)
    return ans

ans = 0
for i in dfs(1, n):
    tmp = 0
    for a, b, c, d in pairs:
        if (i[b-1] - i[a-1]) == c:
            tmp += d
    ans = max(ans, tmp)

print(ans)


# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

def dfs(seq):
    if len(seq) == n:
        yield tuple(seq)
        return
    if len(seq) == 0:
        start = 1
    else:
        start = seq[-1]
    for i in range(start, m+1):
        new_seq = seq.copy()
        new_seq.append(i)
        yield from dfs(new_seq)

ans = 0
for s in dfs([]):
    tmp_ans = 0
    for a, b, c, d in abcd:
        if s[b-1] - s[a-1] == c:
            tmp_ans += d
    ans = max(ans, tmp_ans)
print(ans)
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

def dfs(seq):
    if len(seq) == n:
        yield tuple(seq)
        return
    if len(seq) == 0:
        start = 1
    else:
        start = seq[-1]
    for i in range(start, m+1):
        new_seq = seq.copy()
        new_seq.append(i)
        yield from dfs(new_seq)

ans = 0
for s in dfs([]):
    tmp_ans = 0
    for a, b, c, d in abcd:
        if s[b-1] - s[a-1] == c:
            tmp_ans += d
    ans = max(ans, tmp_ans)
print(ans)


# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

def dfs(seq):
    if len(seq) == n:
        yield seq
        return

    if len(seq) == 0:
        _max = 1
    else:
        _max = seq[-1]
    for i in range(_max, m+1):
        new_seq = seq.copy()
        new_seq.append(i)
        yield from dfs(new_seq)

ans = 0
for s in dfs([]):
    tmp = 0
    for a, b, c, d in abcd:
        if c == s[b-1] - s[a-1]:
            tmp += d
    ans = max(ans, tmp)

print(ans)
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

def dfs(seq):
    if len(seq) == n:
        yield seq
        return
    if not seq:
        _max = 1
    else:
        _max = seq[-1]
    for i in range(_max, m+1):
        new_seq = seq.copy()
        new_seq.append(i)
        yield from dfs(new_seq)

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


# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

def dfs(seq):
    if len(seq) == n:
        yield seq
        return
    if len(seq) == 0:
        _max = 1
    else:
        _max = seq[-1]
    for i in range(_max, m+1):
        new_seq = seq.copy()
        new_seq.append(i)
        yield from dfs(new_seq)

ans = 0
for s in dfs([]):
    tmp_ans = 0
    for a, b, c, d in abcd:
        if (s[b-1] - s[a-1]) == c:
            tmp_ans += d
    ans = max(ans, tmp_ans)
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)]

ans = 0
for pat in combinations_with_replacement(list(range(1, m + 1)), n):
    sm = 0
    for a, b, c, d in abcd:
        a -= 1
        b -= 1
        if pat[b] - pat[a] == c:
            sm += d

    ans = max(ans, sm)

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 pat in combinations_with_replacement(list(range(1, m + 1)), n):
    score = 0
    for a, b, c, d in abcd:
        a -= 1
        b -= 1
        if pat[b] - pat[a] == c:
            score += d

    ans = max(ans, score)

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

if __name__ == "__main__":
    ans = 0
    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()))
    dfs([1])
    print(ans)

# Optimized solution
  
def dfs(A):
    if len(A) == n+1:
        global ans
        total = 0
        for i in range(q):
            if A[b[i]]-A[a[i]] == c[i]:
                total += d[i]
        if total > ans:
            ans = total
        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
import itertools
n,m,q=(int(i) for i in input().split())
l=[]
r=[i+1 for i in range(m)]
point=0
def checker(x,l):
  p=0
  for i in range(len(l)):
    a,b,c,d=(l[i][j] for j in range(4))
    if x[b]-x[a]==c:
      p+=d
  return p
for i in range(q):
  a,b,c,d=(int(i) for i in input().split())
  a-=1
  b-=1
  l.append([a,b,c,d])
for i in itertools.combinations_with_replacement(r,n):
  p=checker(i,l)
  if p>point:
    point=p
print(point)


# Optimized solution
import itertools
n,m,q=(int(i) for i in input().split())
l=[]
r=[i+1 for i in range(m)]
point=0
def checker(x,l):
  p=0
  for i in range(len(l)):
    if x[l[i][1]]-x[l[i][0]]==l[i][2]:
      p+=l[i][3]
  return p
for i in range(q):
  a,b,c,d=(int(i) for i in input().split())
  a-=1
  b-=1
  l.append([a,b,c,d])
for i in itertools.combinations_with_replacement(r,n):
  p=checker(i,l)
  if p>point:
    point=p
print(point)
# Original solution
import itertools 
n, m, q = list(map(int, input().split()))
query = [list(map(int, input().split())) for _ in range(q)]

ls = list(range(n + m - 1))
mx = 0
for l in itertools.combinations(ls, m - 1):
    a = [0]
    idx = 0
    val = 1
    for i in range(n + m - 1): 
        if idx < m - 1 and i == l[idx]:
            val += 1
            idx += 1
        else: 
            a.append(val)
    #print(l, a)
    v = 0
    for i in query:
        if a[i[1]] - a[i[0]] == i[2]: v += i[3]
    mx = max(v, mx)
print(mx)


# Optimized solution
import sys
sys.setrecursionlimit(10**7)

n, m, q = list(map(int, input().split()))
query = [list(map(int, input().split())) for _ in range(q)]

def dfs(k, x):
    global mx
    if k == n:
        v = 0
        for line in query:
            a, b, c, d = line
            if x[b] - x[a] == c: v += d
        if mx < v: mx = v
    else:
        for i in range(x[-1], m + 1):
            dfs(k + 1, x + [i])
    
mx = 0
dfs(0, [1])

print(mx)
# Original solution
n, m, q = list(map(int, input().split()))
queries = [tuple(map(int, input().split())) for _ in range(q)]
arr = []

def f(p):
    if len(p) == n+1:
        arr.append(p[:-1])
        return
    while p[-1] <= m:
        f(p + [p[-1]])
        p[-1] += 1

f([1])

ans = 0
for p in arr:
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    ans = max(ans, score)

print(ans)


# Optimized solution
n, m, q = list(map(int, input().split()))
queries = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0

def calc_score(p):
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    return score

def f(p):
    if len(p) == n+1:
        global ans
        ans = max(ans, calc_score(p))
        return
    while p[-1] <= m:
        f(p + [p[-1]])
        p[-1] += 1

f([1])

print(ans)
# Original solution
n, m, q = list(map(int, input().split()))
queries = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0

def calc_score(p):
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    return score

def f(p):
    if len(p) == n:
        global ans
        ans = max(ans, calc_score(p))
        return
    l = p[-1] if len(p) else 1
    for x in range(l, m+1):
        f(p + [x])

f([])

print(ans)


# Optimized solution
from itertools import combinations_with_replacement as comb

n, m, q = list(map(int, input().split()))
queries = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0

def calc_score(p):
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    return score

for p in comb(list(range(1, m+1)), n):
    ans = max(ans, calc_score(p))

print(ans)
# Original solution
n, m, q = list(map(int, input().split()))
queries = [tuple(map(int, input().split())) for _ in range(q)]

def calc_score(p):
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    return score

def f(p):
    if len(p) == n:
        return calc_score(p)
    ans = 0
    l = p[-1] if len(p) else 1
    for x in range(l, m+1):
        p.append(x)
        ans = max(ans, f(p))
        p.pop()
    return ans

print((f([])))


# Optimized solution
n, m, q = list(map(int, input().split()))
queries = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0
 
def calc_score(p):
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    return score
 
def f(p):
    if len(p) == n:
        global ans
        ans = max(ans, calc_score(p))
        return
    l = p[-1] if len(p) else 1
    for x in range(l, m+1):
        f(p + [x])
 
f([])
 
print(ans)