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



int main() {
   int n, m, q;
   cin >> n >> m >> q;
   vector<int> a(q),b(q),c(q),d(q);
   for (int i=0; i<q; i++) {
      cin >> a[i] >> b[i] >> c[i] >> d[i];
   }
   vector<int> A(n, 0);
   vector<int> cand(1,0);
   for (A[0]=1; A[0]<=m; A[0]++) {
      for (A[1]=A[0]; A[1]<=m; A[1]++) {
         if (n > 2) {
         for (A[2]=A[1]; A[2]<=m; A[2]++) {
            if (n > 3) {
            for (A[3]=A[2]; A[3]<=m; A[3]++) {
               if (n > 4) {
               for (A[4]=A[3]; A[4]<=m; A[4]++) {
                  if (n > 5){
                  for (A[5]=A[4]; A[5]<=m; A[5]++) {
                     if (n > 6){
                     for (A[6]=A[5]; A[6]<=m; A[6]++) {
                        if (n > 7){
                        for (A[7]=A[6]; A[7]<=m; A[7]++) {
                           if (n > 8){
                           for (A[8]=A[7]; A[8]<=m; A[8]++) {
                              if (n > 9){
                              for (A[9]=A[8]; A[9]<=m; A[9]++) {
                                 int sum_d = 0;
                                 for (int i=0; i<q; i++) {
                                    if (A[b[i]-1] - A[a[i]-1] == c[i]) {
                                       sum_d += d[i];
                                    }
                                 }
                                 cand.push_back(sum_d);
                              }
                              }
                              else {
                                 int sum_d = 0;
                                 for (int i=0; i<q; i++) {
                                    if (A[b[i]-1] - A[a[i]-1] == c[i]) {
                                       sum_d += d[i];
                                    }
                                 }
                                 cand.push_back(sum_d);
                              }
                           }
                           }
                           else {
                              int sum_d = 0;
                              for (int i=0; i<q; i++) {
                                 if (A[b[i]-1] - A[a[i]-1] == c[i]) {
                                    sum_d += d[i];
                                 }
                              }
                              cand.push_back(sum_d);
                           }
                        }
                        }
                        else {
                           int sum_d = 0;
                           for (int i=0; i<q; i++) {
                              if (A[b[i]-1] - A[a[i]-1] == c[i]) {
                                 sum_d += d[i];
                              }
                           }
                           cand.push_back(sum_d);
                        }
                     }
                     }
                     else {
                        int sum_d = 0;
                        for (int i=0; i<q; i++) {
                           if (A[b[i]-1] - A[a[i]-1] == c[i]) {
                              sum_d += d[i];
                           }
                        }
                        cand.push_back(sum_d);
                     }
                  }
                  }
                  else {
                     int sum_d = 0;
                     for (int i=0; i<q; i++) {
                        if (A[b[i]-1] - A[a[i]-1] == c[i]) {
                           sum_d += d[i];
                        }
                     }
                     cand.push_back(sum_d);
                  }
               }
               }
               else {
                  int sum_d = 0;
                  for (int i=0; i<q; i++) {
                     if (A[b[i]-1] - A[a[i]-1] == c[i]) {
                        sum_d += d[i];
                     }
                  }
                  cand.push_back(sum_d);
               }
            }
            }
            else {
               int sum_d = 0;
               for (int i=0; i<q; i++) {
                  if (A[b[i]-1] - A[a[i]-1] == c[i]) {
                     sum_d += d[i];
                  }
               }
               cand.push_back(sum_d);
            }
         }
         }
         else {
            int sum_d = 0;
            for (int i=0; i<q; i++) {
               if (A[b[i]-1] - A[a[i]-1] == c[i]) {
                  sum_d += d[i];
               }
            }
            cand.push_back(sum_d);
         }
      }
   }
   cout << *max_element(cand.begin(), cand.end()) << endl;
   return 0;
}

Measured memory_bytes: 3876