#include <bits/stdc++.h>
#include <functional>
using namespace std;

#define st first
#define nd second
#define pb push_back
#define pp pop_back
#define all(x) (x).begin(), (x).end()
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()

typedef long long	ll;
typedef double 	db;
typedef long double ldb;
typedef complex<db> cd;

typedef pair<int, int>	pii;
typedef pair<ll, ll>	pll;
typedef pair<db, db>	pdb;
typedef pair<ldb, ldb>	pldb;
typedef pair<int, ll>	pil;
typedef pair<ll, int>	pli;

typedef vector<int>		vi;
typedef vector<ll>		vl;
typedef vector<string>	vs;
typedef vector<char>	vc;
typedef vector<db>		vdb;
typedef vector<ldb>		vldb;
typedef vector<pii>		vpii;
typedef vector<pll>		vpll;
typedef vector<pdb>		vpdb;
typedef vector<pldb>	vpldb;
typedef vector<pil>		vpil;
typedef vector<pli>		vpli;

template<typename T> T scanf_t();

template<>  int            scanf_t();
template<>  ll             scanf_t();
template<>  db             scanf_t();
template<>  ldb            scanf_t();
template<>  char           scanf_t();
template<>  float          scanf_t();
string      scanf_t(int);  

int     (*si)()     =  scanf_t<int>;
ll      (*sl)()     =  scanf_t<ll>;
db      (*sdb)()    =  scanf_t<db>;
ldb     (*sldb)()   =  scanf_t<ldb>;
char    (*sc)()     =  scanf_t<char>;
float   (*sf)()     =  scanf_t<float>;
string  (*ss)(int)  =  scanf_t;

template<typename T> void printf_t(const T&, char='\n');

template<>  void  printf_t(const  int&,     char);
template<>  void  printf_t(const  ll&,      char);
template<>  void  printf_t(const  db&,      char);
template<>  void  printf_t(const  ldb&,     char);
template<>  void  printf_t(const  float&,   char);
template<>  void  printf_t(const  char&,    char);
template<>  void  printf_t(const  string&,  char);
template<>  void  printf_t(const  char* const&, char);

template<typename T> void print(T);
template<typename T, typename... Args> void print(T, Args...);

template<typename It> void out(It, It, char=' ', char='\n');
template<typename Container> void pv(const Container&, char=' ', char='\n');

void pi(const int&);
void pl(const ll&);
void pd(const db&);
void pld(const ldb&);
void pf(const float&);
void pc(const char&);
void ps(const string&);

ll russian(ll, ll, ll);
ll fastpow(ll, ll, ll);
ll slowpow(ll, ll, ll);
ll extgcd(ll, ll);
ll _gcd(ll, ll);
ll _lcm(ll, ll);
ll _inv(ll, ll);

const  int  INF   =  1e9;
const  ll   LINF  =  1e18;

struct query
{
	int a, b, c, d;
};

const int Q = 50 + 7;
const int N = 10 + 7;
query ques[Q];
int a[N];
int n, m, q, res;

void go(int it, int last)
{
	if (it == n)
	{
		int cur = 0;
		forn (i, q)
			if (a[ques[i].b] - a[ques[i].a] == ques[i].c)
				cur += ques[i].d;
		res = max(res, cur);
		return;
	}
	rep (i, last, m + 1)
	{
		a[it] = i;
		go(it + 1, i);
	}
}

int main()
{
	n = si(); m = si();
	q = si();

	forn (i, q)
		ques[i] = { si() - 1, si() - 1, si(), si() };

	go(0, 1);
	pi(res);

	return 0;
}

template<> int scanf_t() { int x; scanf("%d", &x); return x; }
template<> ll scanf_t() { ll x; scanf("%lld", &x); return x; }
template<> db scanf_t() { db x; scanf("%lf", &x); return x; }
template<> ldb scanf_t() { ldb x; scanf("%Lf", &x); return x; }
template<> char scanf_t() { char x; scanf(" %c", &x); return x; }
template<> float scanf_t() { float x; scanf("%f", &x); return x; }
string scanf_t(int n) { char x[n + 7]; scanf("%s", x); return string(x); }

template<> void printf_t(const int& x, char end) { printf("%d%c", x, end); }
template<> void printf_t(const ll& x, char end) { printf("%lld%c", x, end); }
template<> void printf_t(const db& x, char end) { printf("%lf%c", x, end); }
template<> void printf_t(const ldb& x, char end) { printf("%Lf%c", x, end); }
template<> void printf_t(const float& x, char end) { printf("%f%c", x, end); }
template<> void printf_t(const char& x, char end) { printf("%c%c", x, end); }
template<> void printf_t(const string& x, char end) { printf("%s%c", x.c_str(), end); }
template<> void printf_t(const char* const &x, char end) { printf("%s%c", x, end); }

template<typename T> void print(T last) { printf_t(last); }

template<typename T, typename... Args>
void print(T first, Args... rest)
{
	printf_t(first, ' ');
	print(rest...);
}

template<typename It>
void out(It begin, It end, char sep, char endl)
{
	if (begin == end)
	{
		printf("%c", endl);
		return;
	}	
	while (begin + 1 != end)
		printf_t(*begin++, sep);
	printf_t(*begin, endl);
}

template<typename Container>
void pv(Container& v, char sep, char endl) { out(all(v), sep, endl); }

void  pi(const   int&     x)  {  printf_t(x);  }
void  pl(const   ll&      x)  {  printf_t(x);  }
void  pd(const   db&      x)  {  printf_t(x);  }
void  pld(const  ldb&     x)  {  printf_t(x);  }
void  pf(const   float&   x)  {  printf_t(x);  }
void  pc(const   char&    x)  {  printf_t(x);  }
void  ps(const   string&  x)  {  printf_t(x);  }

ll russian(ll a, ll k, ll m)
{
	ll res = 0LL;
	while (k)
	{
		if (k & 1LL)
			res = (res + a) % m;
		a = (a + a) % m;
	}
	return res;
}

ll fastpow(ll a, ll k, ll m)
{
	ll res = 1LL;
	while (k)
	{
		if (k & 1LL)
			res = (res * a) % m;
		a = (a * a) % m;
		k >>= 1;
	}
	return res;
}

ll slowpow(ll a, ll k, ll m)
{
	ll res = 1LL;
	while (k)
	{
		if (k & 1LL)
			res = russian(res, a, m);
		a = russian(a, a, m);
		k >>= 1;	
	}
	return res;
}

ll _gcd(ll a, ll b)
{
	while (b)
		swap(a %= b, b);
	return a;
}

ll _lcm(ll a, ll b)
{
	return a / _gcd(a, b) * b;
}

ll _inv(ll a, ll p)
{
	return fastpow(a, p - 2, p);
}

ll extgcd(ll a, ll b, ll& k, ll& l)
{
	if (b == 0)
	{
		k = 1; l = 0;
		return a;
	}
	ll res = extgcd(b, a % b, l, k);
	l -= a / b * k;
	return res;
}


Measured memory_bytes: 3784