from sys import stdin,stdout
input=stdin.buffer.readline

ip=[int(x) for x in input().split()]
n=ip[0]
m=ip[1]
q=ip[2]

ai=[]
bi=[]
ci=[]
di=[]
for i in range(q):
	ip=[int(x) for x in input().split()]
	ai.append(ip[0])
	bi.append(ip[1])
	ci.append(ip[2])
	di.append(ip[3])

a=[]
if n==10:
	for i1 in range(1,m+1):
		for i2 in range(i1,m+1):
			for i3 in range(i2,m+1):
				for i4 in range(i3,m+1):
					for i5 in range(i4,m+1):
						for i6 in range(i5,m+1):
							for i7 in range(i6,m+1):
								for i8 in range(i7,m+1):
									for i9 in range(i8,m+1):
										for i10 in range(i9,m+1):
											a.append([i1,i2,i3,i4,i5,i6,i7,i8,i9,i10])

	ans=0
	for i in range(len(a)):
		m=0
		for j in range(q):
			if a[i][bi[j]-1]-a[i][ai[j]-1]==ci[j]:
				m+=di[j]
		if ans<m:
			ans=m

	print(ans)
elif n==9:
	for i1 in range(1,m+1):
		for i2 in range(i1,m+1):
			for i3 in range(i2,m+1):
				for i4 in range(i3,m+1):
					for i5 in range(i4,m+1):
						for i6 in range(i5,m+1):
							for i7 in range(i6,m+1):
								for i8 in range(i7,m+1):
									for i9 in range(i8,m+1):
										a.append([i1,i2,i3,i4,i5,i6,i7,i8,i9])

	ans=0
	for i in range(len(a)):
		m=0
		for j in range(q):
			if a[i][bi[j]-1]-a[i][ai[j]-1]==ci[j]:
				m+=di[j]
		if ans<m:
			ans=m

	print(ans)
elif n==8:
	for i1 in range(1,m+1):
		for i2 in range(i1,m+1):
			for i3 in range(i2,m+1):
				for i4 in range(i3,m+1):
					for i5 in range(i4,m+1):
						for i6 in range(i5,m+1):
							for i7 in range(i6,m+1):
								for i8 in range(i7,m+1):
									a.append([i1,i2,i3,i4,i5,i6,i7,i8])

	ans=0
	for i in range(len(a)):
		m=0
		for j in range(q):
			if a[i][bi[j]-1]-a[i][ai[j]-1]==ci[j]:
				m+=di[j]
		if ans<m:
			ans=m

	print(ans)
elif n==7:
	for i1 in range(1,m+1):
		for i2 in range(i1,m+1):
			for i3 in range(i2,m+1):
				for i4 in range(i3,m+1):
					for i5 in range(i4,m+1):
						for i6 in range(i5,m+1):
							for i7 in range(i6,m+1):
								a.append([i1,i2,i3,i4,i5,i6,i7])

	ans=0
	for i in range(len(a)):
		m=0
		for j in range(q):
			if a[i][bi[j]-1]-a[i][ai[j]-1]==ci[j]:
				m+=di[j]
		if ans<m:
			ans=m

	print(ans)
elif n==6:
	for i1 in range(1,m+1):
		for i2 in range(i1,m+1):
			for i3 in range(i2,m+1):
				for i4 in range(i3,m+1):
					for i5 in range(i4,m+1):
						for i6 in range(i5,m+1):
							a.append([i1,i2,i3,i4,i5,i6])

	ans=0
	for i in range(len(a)):
		m=0
		for j in range(q):
			if a[i][bi[j]-1]-a[i][ai[j]-1]==ci[j]:
				m+=di[j]
		if ans<m:
			ans=m

	print(ans)
elif n==5:
	for i1 in range(1,m+1):
		for i2 in range(i1,m+1):
			for i3 in range(i2,m+1):
				for i4 in range(i3,m+1):
					for i5 in range(i4,m+1):
						a.append([i1,i2,i3,i4,i5])

	ans=0
	for i in range(len(a)):
		m=0
		for j in range(q):
			if a[i][bi[j]-1]-a[i][ai[j]-1]==ci[j]:
				m+=di[j]
		if ans<m:
			ans=m

	print(ans)
elif n==4:
	for i1 in range(1,m+1):
		for i2 in range(i1,m+1):
			for i3 in range(i2,m+1):
				for i4 in range(i3,m+1):
					a.append([i1,i2,i3,i4])

	ans=0
	for i in range(len(a)):
		m=0
		for j in range(q):
			if a[i][bi[j]-1]-a[i][ai[j]-1]==ci[j]:
				m+=di[j]
		if ans<m:
			ans=m

	print(ans)
elif n==3:
	for i1 in range(1,m+1):
		for i2 in range(i1,m+1):
			for i3 in range(i2,m+1):
				a.append([i1,i2,i3])

	ans=0
	for i in range(len(a)):
		m=0
		for j in range(q):
			if a[i][bi[j]-1]-a[i][ai[j]-1]==ci[j]:
				m+=di[j]
		if ans<m:
			ans=m

	print(ans)
elif n==2:
	for i1 in range(1,m+1):
		for i2 in range(i1,m+1):
			a.append([i1,i2])

	ans=0
	for i in range(len(a)):
		m=0
		for j in range(q):
			if a[i][bi[j]-1]-a[i][ai[j]-1]==ci[j]:
				m+=di[j]
		if ans<m:
			ans=m

	print(ans)


Measured memory_bytes: 23096