  #include <bits/stdc++.h>
   using namespace std;
   using pii = pair<int, int>;
   using ll = long long;
   #define rep(i, j, n) for(int i = j; i < (int)(n); i++)
   const ll MOD = 1000000007;
   const ll INFL = (1LL << 60);
   const int INFI = (1 << 30);

   int ans = 0, Q, N, M;
   vector<int> a(100), b(100), c(100), d(100), A(11);

   void dfs(int n) {
     if (n == N) {
       //和を求めてansを更新
       int ans_tmp = 0;
       rep(i, 0, Q) {
         if (A.at(b.at(i)) - A.at(a.at(i)) == c.at(i)) ans_tmp += d.at(i);
       }
       ans = max(ans, ans_tmp);
     }
     else {
       rep(i, A.at(n), M + 1) {
         A.at(n + 1) = i;
         dfs(n + 1);
       }
     }
   }

   int main() {
     cin >> N >> M >> Q;
     A.at(0) = 1;

     rep(i, 0, Q) cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);

     dfs(0);

     cout << ans << endl;
   }
