#include<stdio.h>
int main()
{
	register int i;
	long long ans=0;
	int elect[12]={},a[4][50],n,m,q,cnt;
	scanf("%d %d %d",&n,&m,&q);
	for(i=0;i<q;++i)
		scanf("%d %d %d %d",a[0]+i,a[1]+i,a[2]+i,a[3]+i);
	for(i=1;i<=n;++i)
		elect[i]=1;
	do
	{
		cnt=0;
		for(i=0;i<q;++i)
			cnt+=(elect[a[0][i]]+a[2][i]==elect[a[1][i]]?1:0)*a[3][i];
		ans=ans>cnt?ans:cnt;
		for(i=n;elect[i]>=(m+i-n)&&i;--i);
		elect[i]++;
		for(i++;i<=n;++i)
			elect[i]=elect[i-1];
	}while(elect[0]==0);
	printf("%d",ans);
	return 0;
}