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

int n, m, q;
int a[55], b[55], c[55], d[55];
int A[15];
int ans = 0;

void dfs(int len, int num) {
  if (len == n) {
    int total = 0;
    for (int i = 0; i < q; i++) {
      if (A[b[i]] - A[a[i]] == c[i]) {
        total += d[i];
      }
    }
    ans = max(ans, total);
  } else {
    A[len + 1] = num;
    for (int i = num; i <= m; i++) dfs(len + 1, i);
  }
}

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) cin >> a[i] >> b[i] >> c[i] >> d[i];
  dfs(0, 1);
  cout << ans << endl;
  return 0;
}