#include<bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr ll mod=1000000007;

int N,M,Q,a[50],b[50],c[50],d[50],res;

void dfs(vector<int>v){
	if(v.size()==N){
		int t=0;
		for(int i=0;i<Q;i++){
			if(v[b[i]]-v[a[i]]==c[i]){
				t+=d[i];
			}
		}
		res=max(res,t);
	}
	else{
		if(v.empty())v.push_back(1);
		else v.push_back(v.back());
		for(int i=v.back();i<=M;i++){
			v.back()=i;
			dfs(v);
		}
	}
}

int main(){
	cin>>N>>M>>Q;
	for(int i=0;i<Q;i++){
		cin>>a[i]>>b[i]>>c[i]>>d[i];
		a[i]--;
		b[i]--;
	}
	dfs({});
	cout<<res<<endl;
}
