#include<iostream>
#include<vector>
using namespace std;
#define int long long int
int count=0;
int answer=0;
void function(vector<int> &v,int initial,int final,int prev,int m,int arr[][4],int q)
{
	if(initial>final)
	{
		count++;
		int here=0;
		int j;
		for(j=0;j<q;j++)
		{
			int a=arr[j][0]-1;
			int b=arr[j][1]-1;
			int c=arr[j][2];
			int d=arr[j][3];
			if(v[b]-v[a]==c)
			{
				here+=d;
			}
		}
		answer=max(answer,here);
	}
	else
	{
		int j;
		for(j=prev;j<=m;j++)
		{
			v.push_back(j);
			function(v,initial+1,final,j,m,arr,q);
			v.pop_back();
		}
	}
}
int32_t main()
{
	int n,m,q;
	cin>>n>>m>>q;
	int arr[q][4];
	int j;
	for(j=0;j<q;j++)
	{
		cin>>arr[j][0]>>arr[j][1]>>arr[j][2]>>arr[j][3];
	}
	for(j=1;j<=m;j++)
	{
		vector<int> v;
		v.push_back(j);
		function(v,1,n-1,j,m,arr,q);
	}
	cout<<answer<<endl;
}
