#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEFINE
#include "../../DEBUG.hpp"
#endif
vector<vector<int>> Q;
vector<int> seq;
int n, m, q;
int ans;
// this function recursively builds the sequence of length n
// i means we are at the ith position in the sequence and num
// means that element at position i >= num (remember that the sequence has to be
// non decreasing.
void solve(int i, int num) {
  if (i == n) {
  // this means we have already made a sequence of length n
  // which is currently stored in seq
  // now just use brute force to check how many conditions of Q are satisfied and
  // accordingly update ans.
    int temp = 0;
    for (int j = 0; j < q; j++) {
      if (seq[Q[j][1] - 1] - seq[Q[j][0] - 1] == Q[j][2]) {
        temp += Q[j][3];
      }
    }
    ans = max(temp, ans);
    return;
  }
  // else we have not yet completed the sequence making process.
  // we can place all, numbers from num to m at the position i.
  for (int j = num; j <= m; j++) {
    seq.push_back(j);
    // every time, we place a number at the ith position we, try
    // building the rest, of the sequence, update the answer, remove the 
    // last inserted element and go for the next j.
    solve(i + 1, j);
    seq.pop_back();
  }
}

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ans = 0;
  cin >> n >> m >> q;
  Q.resize(q, vector<int>(4));
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < 4; j++) {
      cin >> Q[i][j];
    }
  }
  solve(0, 1);
  cout << ans << '\n';
  return 0;
}