#include <algorithm>
#include <bitset>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define ll long long
#define PLL pair<ll,ll>
#define rep(i,n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i,n) for (ll i = 1; i <= (ll)(n); i++)
#define repR(i,n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define rep1R(i,n) for (ll i = (ll)(n); i >= 1; i--)
#define loop(i,a,B) for (ll i = a; i B; i++)
#define loopR(i,a,B) for (ll i = a; i B; i--)
#define all(v) v.begin(), v.end()
#define fcout cout<<fixed<<setprecision(15)
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078160628620899
const unsigned int BIT_FLAG_0 = (1 << 0);
const unsigned int BIT_FLAG_1 = (1 << 1);
const unsigned int BIT_FLAG_2 = (1 << 2);
const unsigned int BIT_FLAG_3 = (1 << 3);
const unsigned int BIT_FLAG_4 = (1 << 4);
const unsigned int BIT_FLAG_5 = (1 << 5);
const unsigned int BIT_FLAG_6 = (1 << 6);
const unsigned int BIT_FLAG_7 = (1 << 7);
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll INF = 1000000000000000000;
 
string S, T, U;
char V[1100000];
ll ANS = 0, SUM = 0, NOW = 0, N, M, A, B, C, D, E, F[1100000] = { 0 }, G[1100000] = { 0 }, H[1100000] = { 0 }, I[1100000] = { 0 };
double n, m, a, b, c, d, e;
bool ans = 0, now = 0, sum = 0;
PLL ARM[1100000];
int main(void) {
	cin >> N >> M >> A;
	rep(i, A) cin >> F[i] >> G[i] >> H[i] >> I[i];
	ANS = 0;
	if (N == 10) {
		ll Z[15];
		for (Z[1]= 1; Z[1] <= M; Z[1]++)for (Z[2] = Z[1]; Z[2] <= M; Z[2]++)for (Z[3] = Z[2]; Z[3] <= M; Z[3]++)
			for (Z[4] = Z[3]; Z[4] <= M; Z[4]++)for (Z[5] = Z[4]; Z[5] <= M; Z[5]++)for (Z[6] = Z[5]; Z[6] <= M; Z[6]++)
			for (Z[7] = Z[6]; Z[7] <= M; Z[7]++)for (Z[8] = Z[7]; Z[8] <= M; Z[8]++)for (Z[9] = Z[8]; Z[9] <= M; Z[9]++)for (Z[10] = Z[9]; Z[10] <= M; Z[10]++) {
				SUM = 0;
				rep(i, A) {
					if (Z[G[i]] - Z[F[i]] == H[i]) SUM += I[i];
				}
				ANS = max(ANS, SUM);
			}
	}
	if (N == 9) {
		ll Z[15];
		for (Z[1] = 1; Z[1] <= M; Z[1]++)for (Z[2] = Z[1]; Z[2] <= M; Z[2]++)for (Z[3] = Z[2]; Z[3] <= M; Z[3]++)
			for (Z[4] = Z[3]; Z[4] <= M; Z[4]++)for (Z[5] = Z[4]; Z[5] <= M; Z[5]++)for (Z[6] = Z[5]; Z[6] <= M; Z[6]++)
				for (Z[7] = Z[6]; Z[7] <= M; Z[7]++)for (Z[8] = Z[7]; Z[8] <= M; Z[8]++)for (Z[9] = Z[8]; Z[9] <= M; Z[9]++) {
					SUM = 0;
					rep(i, A) {
						if (Z[G[i]] - Z[F[i]] == H[i]) SUM += I[i];
					}
					ANS = max(ANS, SUM);
				}
	}
	if (N == 8) {
		ll Z[15];
		for (Z[1] = 1; Z[1] <= M; Z[1]++)for (Z[2] = Z[1]; Z[2] <= M; Z[2]++)for (Z[3] = Z[2]; Z[3] <= M; Z[3]++)
			for (Z[4] = Z[3]; Z[4] <= M; Z[4]++)for (Z[5] = Z[4]; Z[5] <= M; Z[5]++)for (Z[6] = Z[5]; Z[6] <= M; Z[6]++)
				for (Z[7] = Z[6]; Z[7] <= M; Z[7]++)for (Z[8] = Z[7]; Z[8] <= M; Z[8]++) {
					SUM = 0;
					rep(i, A) {
						if (Z[G[i]] - Z[F[i]] == H[i]) SUM += I[i];
					}
					ANS = max(ANS, SUM);
				}
	}
	if (N == 7) {
		ll Z[15];
		for (Z[1] = 1; Z[1] <= M; Z[1]++)for (Z[2] = Z[1]; Z[2] <= M; Z[2]++)for (Z[3] = Z[2]; Z[3] <= M; Z[3]++)
			for (Z[4] = Z[3]; Z[4] <= M; Z[4]++)for (Z[5] = Z[4]; Z[5] <= M; Z[5]++)for (Z[6] = Z[5]; Z[6] <= M; Z[6]++)
				for (Z[7] = Z[6]; Z[7] <= M; Z[7]++) {
					SUM = 0;
					rep(i, A) {
						if (Z[G[i]] - Z[F[i]] == H[i]) SUM += I[i];
					}
					ANS = max(ANS, SUM);
				}
	}
	if (N == 6) {
		ll Z[15];
		for (Z[1] = 1; Z[1] <= M; Z[1]++)for (Z[2] = Z[1]; Z[2] <= M; Z[2]++)for (Z[3] = Z[2]; Z[3] <= M; Z[3]++)
			for (Z[4] = Z[3]; Z[4] <= M; Z[4]++)for (Z[5] = Z[4]; Z[5] <= M; Z[5]++)for (Z[6] = Z[5]; Z[6] <= M; Z[6]++) {
					SUM = 0;
					rep(i, A) {
						if (Z[G[i]] - Z[F[i]] == H[i]) SUM += I[i];
					}
					ANS = max(ANS, SUM);
				}
	}
	if (N == 5) {
		ll Z[15];
		for (Z[1] = 1; Z[1] <= M; Z[1]++)for (Z[2] = Z[1]; Z[2] <= M; Z[2]++)for (Z[3] = Z[2]; Z[3] <= M; Z[3]++)
			for (Z[4] = Z[3]; Z[4] <= M; Z[4]++)for (Z[5] = Z[4]; Z[5] <= M; Z[5]++) {
					SUM = 0;
					rep(i, A) {
						if (Z[G[i]] - Z[F[i]] == H[i]) SUM += I[i];
					}
					ANS = max(ANS, SUM);
				}
	}
	if (N == 4) {
		ll Z[15];
		for (Z[1] = 1; Z[1] <= M; Z[1]++)for (Z[2] = Z[1]; Z[2] <= M; Z[2]++)for (Z[3] = Z[2]; Z[3] <= M; Z[3]++)
			for (Z[4] = Z[3]; Z[4] <= M; Z[4]++) {
					SUM = 0;
					rep(i, A) {
						if (Z[G[i]] - Z[F[i]] == H[i]) SUM += I[i];
					}
					ANS = max(ANS, SUM);
				}
	}
	if (N == 3) {
		ll Z[15];
		for (Z[1] = 1; Z[1] <= M; Z[1]++)for (Z[2] = Z[1]; Z[2] <= M; Z[2]++)for (Z[3] = Z[2]; Z[3] <= M; Z[3]++) {
					SUM = 0;
					rep(i, A) {
						if (Z[G[i]] - Z[F[i]] == H[i]) SUM += I[i];
					}
					ANS = max(ANS, SUM);
				}
	}
	if (N == 2) {
		ll Z[15];
		for (Z[1] = 1; Z[1] <= M; Z[1]++)for (Z[2] = Z[1]; Z[2] <= M; Z[2]++) {
					SUM = 0;
					rep(i, A) {
						if (Z[G[i]] - Z[F[i]] == H[i]) SUM += I[i];
					}
					ANS = max(ANS, SUM);
				}
	}
	cout << ANS << endl;
	return 0;
}

Measured memory_bytes: 3620