// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
// #include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
typedef long double ld;
#define REP(i,n) for(ll i=0; i<(n); i++)
#define REPR(i, n) for(ll i = n;i >= 0;i--)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
#define ALL(n) begin(n),end(n)
#define IN(a, x, b) (a<=x && x<b)
#define INIT std::ios::sync_with_stdio(false);std::cin.tie(0);
template<class T> inline T CHMAX(T & a, const T b) { return a = (a < b) ? b : a; }
template<class T> inline T CHMIN(T & a, const T b) { return a = (a > b) ? b : a; }

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
const long long MOD = 1000000007;

ll n;
ll m;
ll q;
ll a[100];
ll b[100];
ll c[100];
ll d[100];
ll array_base[10];


int main(void){
    ll ans=0;
    cin >> n;
    cin >> m;
    cin >> q;
    REP(i,q){
        cin >> a[i] >> b[i] >> c[i] >> d[i];
    }

    for(ll i1=1; i1<=10; i1++){
        array_base[0] = i1;
        if(i1 > m){break;}
        for(ll i2=i1; i2<=10; i2++){
        array_base[1] = i2;
        if(i2 > m){break;}
        for(ll i3=i2; i3<=10; i3++){
        array_base[2] = i3;
        if(i3 > m){break;}
        for(ll i4=i3; i4<=10; i4++){
        array_base[3] = i4;
        if(i4 > m){break;}
        for(ll i5=i4; i5<=10; i5++){
        array_base[4] = i5;
        if(i5 > m){break;}
        for(ll i6=i5; i6<=10; i6++){
        array_base[5] = i6;
        if(i6 > m){break;}
        for(ll i7=i6; i7<=10; i7++){
        array_base[6] = i7;
        if(i7 > m){break;}
        for(ll i8=i7; i8<=10; i8++){
        array_base[7] = i8;
        if(i8 > m){break;}
        for(ll i9=i8; i9<=10; i9++){
        array_base[8] = i9;
        if(i9 > m){break;}
        for(ll i10=i9; i10<=10; i10++){
        array_base[9] = i10;
        if(i10 > m){break;}


        ll tmp_ans=0;
        REP(i,n-1){
            if(array_base[i] > array_base[i+1]){
                continue;
            }
        }

        REP(i,q){
            if( (array_base[b[i]-1] - array_base[a[i]-1]) == c[i]){
                tmp_ans += d[i];
            }
        }

        CHMAX(ans, tmp_ans);
        }
        }
        }
        }
        }
        }
        }
        }
        }
    }


    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3640