/*
* @Author: Arextre
* @Date:   2020-05-02 20:33:36
* @Last Modified by:   Arextre
* @Last Modified time: 2020-05-02 21:27:45
*/
#include<cstdio>
#include<algorithm>
using namespace std;

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define FILEOI
// #define int long long
// #define int unsigned
// #define int unsigned long long

#ifdef FILEOI
# define MAXBUFFERSIZE 500000
inline char fgetc(){
    static char buf[MAXBUFFERSIZE+5],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXBUFFERSIZE,stdin),p1==p2)?EOF:*p1++;
}
# undef MAXBUFFERSIZE
# define cg (c=fgetc())
#else
# define cg (c=getchar())
#endif
template<class T>inline void qread(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
inline int qread(){
    int x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=10;
const int INF=(1<<30)-1;
const int MAXQ=50;

int n,m,q,ans;

struct require{
	int a,b,c,d;
	bool operator < (const require rhs)const{return d>rhs.d;}
}t[MAXQ+5];

int a[MAXN+5];
/*
bool chk(){
	int pre=-INF,maxx=-INF,minn=INF;
	for(int i=1;i<=n;++i)if(a[i]!=-INF){
		if(a[i]<pre)return false;
		pre=Max(pre,a[i]);
		maxx=Max(a[i],maxx);
		minn=Min(minn,a[i]);
	}
	if(maxx==-INF && minn==INF)return true;
	if(maxx-minn>=m)return false;
	return true;
}

inline void dfs(const int now,const int pts){
	if(chk())ans=Max(ans,pts);
	else return;
	if(now==q+1)return;
	for(int i=now;i<=q;++i){
		if(a[t[i].a]==-INF && a[t[i].b]==-INF){
			a[t[i].a]=0,a[t[i].b]=t[i].c;
			dfs(i+1,pts+t[i].d);
			a[t[i].a]=a[t[i].b]=-INF;
		}
		else if(a[t[i].a]==-INF){
			a[t[i].a]=a[t[i].b]-t[i].c;
			dfs(i+1,pts+t[i].d);
			a[t[i].a]=-INF;
		}
		else if(a[t[i].b]==-INF){
			a[t[i].b]=a[t[i].a]+t[i].c;
			dfs(i+1,pts+t[i].d);
			a[t[i].b]=-INF;
		}
		else{
			if(a[t[i].a]+t[i].c!=a[t[i].b])continue;
			dfs(i+1,pts+t[i].d);
		}
	}
}
*/

inline int calc(){
	int ret=0;
	for(int i=1;i<=q;++i)if(a[t[i].b]-a[t[i].a]==t[i].c)
		ret+=t[i].d;
	return ret;
}

void dfs(const int now,const int pre){
	if(now==n+1){
		ans=Max(ans,calc());
		return;
	}
	for(int i=pre;i<=m;++i){
		a[now]=i;
		dfs(now+1,i);
	}
}

signed main(){
#ifdef FILEOI
    freopen("file.in","r",stdin);
    freopen("file.out","w",stdout);
#endif
    qread(n,m,q);
    for(int i=1;i<=q;++i)qread(t[i].a,t[i].b,t[i].c,t[i].d);
    // for(int i=1;i<=n;++i)a[i]=-INF;
    // sort(t+1,t+q+1);
	dfs(1,1);
	printf("%d\n",ans);
    return 0;
}

Measured memory_bytes: 3228