#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int>a,b,c,d;
int n,m,q;
int ans=0;
void dfs(vector<int>A)
{
if(A.size()==n+1)
{
int temp=0;
for(int i=0;i<q;i++)
{
    if(A[b[i]]-A[a[i]]==c[i])temp+=d[i];
}ans =max(ans,temp);return;

}
A.push_back(A.back());
while(A.back()<=m)
{
dfs(A);
A.back()++;

}

}

int main()
{

cin>>n>>m>>q;
a=b=c=d=vector<int>(q);
for(int i=0;i<q;i++)
cin>>a[i]>>b[i]>>c[i]>>d[i];

dfs(vector<int>(1,1));
cout <<ans;
}