#include <bits/stdc++.h>
using namespace std;
#define FOR(i, begin, end) for (ll i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (ll i = (begin)-1; i >= (end); i--)
#define IREP(i, n) IFOR(i, n, 0)
#define SORT(a) sort(a.begin(), a.end())
#define ISORT(a) sort(a.begin(), a.end(), greater<ll>())
#define REVERSE(a) reverse(a.begin(), a.end())
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v)                                                              \
  cout << #v << "=(" << v[0];                                                  \
  FOR(i_debug, 1, v.size()) { cout << "," << v[i_debug]; }                     \
  cout << ")" << endl;
#define mdebug(m)                                                              \
  cout << #m << "=" << endl;                                                   \
  REP(i_debug, m.size()) {                                                     \
    REP(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; }    \
    cout << endl;                                                              \
  }
#define Lower_bound(v, x)                                                      \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)

#define ll long long
// #define f first
// #define s second
#define mp make_pair
#define pb push_back
#define INF 1000000000000000000

using vec = vector<ll>;
using mat = vector<vec>;
using Pii = pair<ll, ll>;
using PiP = pair<ll, Pii>;
using PPi = pair<Pii, ll>;
using bvec = vector<bool>;
using Pvec = vector<Pii>;

template <typename T> void readv(vector<T> &a) { REP(i, a.size()) cin >> a[i]; }
void readv_m1(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}

ll mod = 1000000007;

int main() {
  ll N, M, Q;
  cin >> N >> M >> Q;
  vector<vector<ll>> a(Q, vec(4));

  REP(i, Q) {
    cin >> a[i][0] >> a[i][1] >> a[i][2] >> a[i][3];
    a[i][0]--;
    a[i][1]--;
  }
  // SORT(a);
  vector<vec> dp(N + 1, vec(M, 0));
  vec A(10, 0);

  ll ans = 0;

  FOR(i1, 1, M + 1) {
    A[0] = i1;
    FOR(i2, i1, M + 1) {
      A[1] = i2;
      FOR(i3, i2, M + 1) {
        A[2] = i3;
        FOR(i4, i3, M + 1) {
          A[3] = i4;
          FOR(i5, i4, M + 1) {
            A[4] = i5;
            FOR(i6, i5, M + 1) {
              A[5] = i6;
              FOR(i7, i6, M + 1) {
                A[6] = i7;
                FOR(i8, i7, M + 1) {
                  A[7] = i8;
                  FOR(i9, i8, M + 1) {
                    A[8] = i9;
                    FOR(i10, i9, M + 1) {
                      A[9] = i10;
                      ll tempans = 0;
                      REP(q, Q) {
                        if (A[a[q][1]] - A[a[q][0]] == a[q][2])
                          tempans += a[q][3];
                      }
                      ans = max(ans, tempans);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 3640