#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <deque>     // deque
#include <iomanip>   //setprecsion
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <utility>   // pair, make_pair
#include <vector>    // vector

#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath>  //sin, cos, tan

using namespace std;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define blank(ans) cout << (ans) << " ";
#define mp(p, q) make_pair(p, q)
#define mt(p, q, r) make_tuple(p, q, r)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef string str;

typedef vector<ll> vll;
typedef vector<ld> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<vector<ll>> vvll;
typedef vector<vector<ld>> vvd;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<vector<str>> vvs;
typedef vector<pair<ll, ll>> vpll;
typedef vector<tuple<ll, ll, ll>> vtlll;

const ld PI = acos(-1.0);
const ll MAX = 9000000000000000000;
const ll MIN = -9000000000000000000;
const ld DMAX = 4500;
const ld DMIN = -4500;
const ll MOD = 1000000007;

template <typename T>
void fin(T a)
{
  cout << a << endl;
  exit(0);
}

void Main()
{
  ll n, m, q;
  cin >> n >> m >> q;
  vll a(q), b(q), c(q), d(q);
  rep(i, q) cin >> a[i] >> b[i] >> c[i] >> d[i];
  ll ans = 0;
  vll A(n);

  if (n == 2)
  {
    for (ll i = 1; i <= m; i++)
    {
      for (ll j = i; j <= m; j++)
      {
        A[0] = i;
        A[1] = j;
        ll flag = 0;
        rep(Q, q)
        {
          if (A[b[Q] - 1] - A[a[Q] - 1] == c[Q])
            flag += d[Q];
        }
        ans = max(flag, ans);
      }
    }
  }
  else if (n == 3)
  {
    for (ll i = 1; i <= m; i++)
    {
      for (ll j = i; j <= m; j++)
      {
        for (ll k = j; k <= m; k++)
        {
          A[0] = i;
          A[1] = j;
          A[2] = k;
          ll flag = 0;
          rep(Q, q)
          {
            if (A[b[Q] - 1] - A[a[Q] - 1] == c[Q])
              flag += d[Q];
          }
          ans = max(flag, ans);
        }
      }
    }
  }
  else if (n == 4)
  {
    for (ll i = 1; i <= m; i++)
    {
      for (ll j = i; j <= m; j++)
      {
        for (ll k = j; k <= m; k++)
        {
          for (ll h = k; h <= m; h++)
          {
            A[0] = i;
            A[1] = j;
            A[2] = k;
            A[3] = h;
            ll flag = 0;
            rep(Q, q)
            {
              if (A[b[Q] - 1] - A[a[Q] - 1] == c[Q])
                flag += d[Q];
            }
            ans = max(flag, ans);
          }
        }
      }
    }
  }
  else if (n == 5)
  {
    for (ll i = 1; i <= m; i++)
    {
      for (ll j = i; j <= m; j++)
      {
        for (ll k = j; k <= m; k++)
        {
          for (ll h = k; h <= m; h++)
          {
            for (ll t = h; t <= m; t++)
            {
              A[0] = i;
              A[1] = j;
              A[2] = k;
              A[3] = h;
              A[4] = t;
              ll flag = 0;
              rep(Q, q)
              {
                if (A[b[Q] - 1] - A[a[Q] - 1] == c[Q])
                  flag += d[Q];
              }
              ans = max(flag, ans);
            }
          }
        }
      }
    }
  }
  else if (n == 6)
  {
    for (ll i = 1; i <= m; i++)
    {
      for (ll j = i; j <= m; j++)
      {
        for (ll k = j; k <= m; k++)
        {
          for (ll h = k; h <= m; h++)
          {
            for (ll t = h; t <= m; t++)
            {
              for (ll r = t; r <= m; r++)
              {
                A[0] = i;
                A[1] = j;
                A[2] = k;
                A[3] = h;
                A[4] = t;
                A[5] = r;
                ll flag = 0;
                rep(Q, q)
                {
                  if (A[b[Q] - 1] - A[a[Q] - 1] == c[Q])
                    flag += d[Q];
                }
                ans = max(flag, ans);
              }
            }
          }
        }
      }
    }
  }
  else if (n == 7)
  {
    for (ll i = 1; i <= m; i++)
    {
      for (ll j = i; j <= m; j++)
      {
        for (ll k = j; k <= m; k++)
        {
          for (ll h = k; h <= m; h++)
          {
            for (ll t = h; t <= m; t++)
            {
              for (ll r = t; r <= m; r++)
              {
                for (ll f = r; f <= m; f++)
                {
                  A[0] = i;
                  A[1] = j;
                  A[2] = k;
                  A[3] = h;
                  A[4] = t;
                  A[5] = r;
                  A[6] = f;
                  ll flag = 0;
                  rep(Q, q)
                  {
                    if (A[b[Q] - 1] - A[a[Q] - 1] == c[Q])
                      flag += d[Q];
                  }
                  ans = max(flag, ans);
                }
              }
            }
          }
        }
      }
    }
  }
  else if (n == 8)
  {
    for (ll i = 1; i <= m; i++)
    {
      for (ll j = i; j <= m; j++)
      {
        for (ll k = j; k <= m; k++)
        {
          for (ll h = k; h <= m; h++)
          {
            for (ll t = h; t <= m; t++)
            {
              for (ll r = t; r <= m; r++)
              {
                for (ll f = r; f <= m; f++)
                {
                  for (ll w = f; w <= m; w++)
                  {
                    A[0] = i;
                    A[1] = j;
                    A[2] = k;
                    A[3] = h;
                    A[4] = t;
                    A[5] = r;
                    A[6] = f;
                    A[7] = w;
                    ll flag = 0;
                    rep(Q, q)
                    {
                      if (A[b[Q] - 1] - A[a[Q] - 1] == c[Q])
                        flag += d[Q];
                    }
                    ans = max(flag, ans);
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  else if (n == 9)
  {
    for (ll i = 1; i <= m; i++)
    {
      for (ll j = i; j <= m; j++)
      {
        for (ll k = j; k <= m; k++)
        {
          for (ll h = k; h <= m; h++)
          {
            for (ll t = h; t <= m; t++)
            {
              for (ll r = t; r <= m; r++)
              {
                for (ll f = r; f <= m; f++)
                {
                  for (ll w = f; w <= m; w++)
                  {
                    for (ll e = w; e <= m; e++)
                    {
                      A[0] = i;
                      A[1] = j;
                      A[2] = k;
                      A[3] = h;
                      A[4] = t;
                      A[5] = r;
                      A[6] = f;
                      A[7] = w;
                      A[8] = e;
                      ll flag = 0;
                      rep(Q, q)
                      {
                        if (A[b[Q] - 1] - A[a[Q] - 1] == c[Q])
                          flag += d[Q];
                      }
                      ans = max(flag, ans);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  else
  {
    for (ll i = 1; i <= m; i++)
    {
      for (ll j = i; j <= m; j++)
      {
        for (ll k = j; k <= m; k++)
        {
          for (ll h = k; h <= m; h++)
          {
            for (ll t = h; t <= m; t++)
            {
              for (ll r = t; r <= m; r++)
              {
                for (ll f = r; f <= m; f++)
                {
                  for (ll w = f; w <= m; w++)
                  {
                    for (ll e = w; e <= m; e++)
                    {
                      for (ll p = e; p <= m; p++)
                      {
                        A[0] = i;
                        A[1] = j;
                        A[2] = k;
                        A[3] = h;
                        A[4] = t;
                        A[5] = r;
                        A[6] = f;
                        A[7] = w;
                        A[8] = e;
                        A[9] = p;
                        ll flag = 0;
                        rep(Q, q)
                        {
                          if (A[b[Q] - 1] - A[a[Q] - 1] == c[Q])
                            flag += d[Q];
                        }
                        ans = max(flag, ans);
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  fin(ans);
}

//前処理
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20); //高精度少数表示
  //cout << setfill('0') << internal << setw(4); 0埋め４桁
  Main();
  return 0;
}


Measured memory_bytes: 3660