#include <bits/stdc++.h>

using namespace std;


 
int ctoi(const char c){
  switch(c){
    case '0': return 0;
    case '1': return 1;
    case '2': return 2;
    case '3': return 3;
    case '4': return 4;
    case '5': return 5;
    case '6': return 6;
    case '7': return 7;
    case '8': return 8;
    case '9': return 9;
    default : return -1;
  }
}

//5
int gcd(int a,int b){return b?gcd(b,a%b):a;}


int main(){

ios::sync_with_stdio(false);
cin.tie(0);
int n,m,q;

cin>>n>>m>>q;

int a[q],b[q],c[q],d[q];

for(int i=0;i<q;i++){
cin>>a[i]>>b[i]>>c[i]>>d[i];
}


if(n==2){

int mini=0;

int aa[2];
for(aa[0]=1;aa[0]<=m;aa[0]++){
for(aa[1]=aa[0];aa[1]<=m;aa[1]++){
int y=0;
for(int z=0;z<q;z++){
if(aa[b[z]-1]-aa[a[z]-1]==c[z])y+=d[z];
}
mini=max(mini,y);


}
}
cout<<mini<<endl;
}else if(n==3){

int mini=0;

int aa[3];
for(aa[0]=1;aa[0]<=m;aa[0]++){
for(aa[1]=aa[0];aa[1]<=m;aa[1]++){
for(aa[2]=aa[1];aa[2]<=m;aa[2]++){

int y=0;
for(int z=0;z<q;z++){
if(aa[b[z]-1]-aa[a[z]-1]==c[z])y+=d[z];
}
mini=max(mini,y);


}
}
}
cout<<mini<<endl;


}else if(n==4){

int mini=0;

int aa[4];
for(aa[0]=1;aa[0]<=m;aa[0]++){
for(aa[1]=aa[0];aa[1]<=m;aa[1]++){
for(aa[2]=aa[1];aa[2]<=m;aa[2]++){
for(aa[3]=aa[2];aa[3]<=m;aa[3]++){

int y=0;
for(int z=0;z<q;z++){
if(aa[b[z]-1]-aa[a[z]-1]==c[z])y+=d[z];
}
mini=max(mini,y);


}
}
}
}
cout<<mini<<endl;


}else if(n==5){

int mini=0;

int aa[5];
for(aa[0]=1;aa[0]<=m;aa[0]++){
for(aa[1]=aa[0];aa[1]<=m;aa[1]++){
for(aa[2]=aa[1];aa[2]<=m;aa[2]++){
for(aa[3]=aa[2];aa[3]<=m;aa[3]++){
for(aa[4]=aa[3];aa[4]<=m;aa[4]++){

int y=0;
for(int z=0;z<q;z++){
if(aa[b[z]-1]-aa[a[z]-1]==c[z])y+=d[z];
}
mini=max(mini,y);


}
}
}
}
}
cout<<mini<<endl;


}else if(n==6){

int mini=0;

int aa[6];
for(aa[0]=1;aa[0]<=m;aa[0]++){
for(aa[1]=aa[0];aa[1]<=m;aa[1]++){
for(aa[2]=aa[1];aa[2]<=m;aa[2]++){
for(aa[3]=aa[2];aa[3]<=m;aa[3]++){
for(aa[4]=aa[3];aa[4]<=m;aa[4]++){
for(aa[5]=aa[4];aa[5]<=m;aa[5]++){

int y=0;
for(int z=0;z<q;z++){
if(aa[b[z]-1]-aa[a[z]-1]==c[z])y+=d[z];
}
mini=max(mini,y);


}
}
}
}
}
}
cout<<mini<<endl;


}else if(n==7){

int mini=0;

int aa[7];
for(aa[0]=1;aa[0]<=m;aa[0]++){
for(aa[1]=aa[0];aa[1]<=m;aa[1]++){
for(aa[2]=aa[1];aa[2]<=m;aa[2]++){
for(aa[3]=aa[2];aa[3]<=m;aa[3]++){
for(aa[4]=aa[3];aa[4]<=m;aa[4]++){
for(aa[5]=aa[4];aa[5]<=m;aa[5]++){
for(aa[6]=aa[5];aa[6]<=m;aa[6]++){

int y=0;
for(int z=0;z<q;z++){
if(aa[b[z]-1]-aa[a[z]-1]==c[z])y+=d[z];
}
mini=max(mini,y);


}
}
}
}
}
}
}
cout<<mini<<endl;


}else if(n==8){

int mini=0;

int aa[8];
for(aa[0]=1;aa[0]<=m;aa[0]++){
for(aa[1]=aa[0];aa[1]<=m;aa[1]++){
for(aa[2]=aa[1];aa[2]<=m;aa[2]++){
for(aa[3]=aa[2];aa[3]<=m;aa[3]++){
for(aa[4]=aa[3];aa[4]<=m;aa[4]++){
for(aa[5]=aa[4];aa[5]<=m;aa[5]++){
for(aa[6]=aa[5];aa[6]<=m;aa[6]++){
for(aa[7]=aa[6];aa[7]<=m;aa[7]++){

int y=0;
for(int z=0;z<q;z++){
if(aa[b[z]-1]-aa[a[z]-1]==c[z])y+=d[z];
}
mini=max(mini,y);


}
}
}
}
}
}
}
}
cout<<mini<<endl;


}else if(n==9){

int mini=0;

int aa[9];
for(aa[0]=1;aa[0]<=m;aa[0]++){
for(aa[1]=aa[0];aa[1]<=m;aa[1]++){
for(aa[2]=aa[1];aa[2]<=m;aa[2]++){
for(aa[3]=aa[2];aa[3]<=m;aa[3]++){
for(aa[4]=aa[3];aa[4]<=m;aa[4]++){
for(aa[5]=aa[4];aa[5]<=m;aa[5]++){
for(aa[6]=aa[5];aa[6]<=m;aa[6]++){
for(aa[7]=aa[6];aa[7]<=m;aa[7]++){
for(aa[8]=aa[7];aa[8]<=m;aa[8]++){

int y=0;
for(int z=0;z<q;z++){
if(aa[b[z]-1]-aa[a[z]-1]==c[z])y+=d[z];
}
mini=max(mini,y);


}
}
}
}
}
}
}
}
}
cout<<mini<<endl;


}else if(n==10){

int mini=0;

int aa[10];
for(aa[0]=1;aa[0]<=m;aa[0]++){
for(aa[1]=aa[0];aa[1]<=m;aa[1]++){
for(aa[2]=aa[1];aa[2]<=m;aa[2]++){
for(aa[3]=aa[2];aa[3]<=m;aa[3]++){
for(aa[4]=aa[3];aa[4]<=m;aa[4]++){
for(aa[5]=aa[4];aa[5]<=m;aa[5]++){
for(aa[6]=aa[5];aa[6]<=m;aa[6]++){
for(aa[7]=aa[6];aa[7]<=m;aa[7]++){
for(aa[8]=aa[7];aa[8]<=m;aa[8]++){
for(aa[9]=aa[8];aa[9]<=m;aa[9]++){

int y=0;
for(int z=0;z<q;z++){
if(aa[b[z]-1]-aa[a[z]-1]==c[z])y+=d[z];
}
mini=max(mini,y);


}
}
}
}
}
}
}
}
}
}
cout<<mini<<endl;


}



}



//cout << fixed << setprecision(5) <<(n*2)*M_PI << endl;


Measured memory_bytes: 3656