#include<bits/stdc++.h>
using namespace std;
struct node{
	int a,b,c,d;
}A[51];
int now[51],ans=-1;
int main(){
    int n,m,q,s=0;
    cin>>n>>m>>q;
	for(int i=1;i<=q;i++)
		cin>>A[i].a>>A[i].b>>A[i].c>>A[i].d;
	for(int i=1;i<n;i++)
	    now[i]=1;
	while(now[0]==0){
		int sum=0;
		for(int i=1;i<=q;i++)
			if(now[A[i].b]-now[A[i].a]==A[i].c)
			    sum+=A[i].d;
		ans=max(ans,sum);
		for(int i=n;i>=0;i--){
			if(now[i]<m){
				now[i]++;
				for(int j=i+1;j<=n;j++)
				    now[j]=now[j-1];
				break;
			}
		}
	}
	cout<<ans;
    return 0;
} 