// Original C++ solution (slower):


#include <iostream>

#include <vector>

#include <map>

#include <cmath>

#include <algorithm>

using namespace std;

int main()

{

    int n,m,q;

    cin >> n >> m >> q;

    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];

    }

    

    int cnt = 0;

    std::vector<int> v[100000];

    for(int i0=0; i0<m; i0++){

    for(int i1=i0; i1<m; i1++){

    for(int i2=i1; i2<m; i2++){

    for(int i3=i2; i3<m; i3++){

    for(int i4=i3; i4<m; i4++){

    for(int i5=i4; i5<m; i5++){

    for(int i6=i5; i6<m; i6++){

    for(int i7=i6; i7<m; i7++){

    for(int i8=i7; i8<m; i8++){

    for(int i9=i8; i9<m; i9++){

        v[cnt] = {i0, i1, i2, i3, i4, i5, i6, i7, i8, i9}; 

        cnt++;

    }}}}}}}}}}

    

    long long int max = 0;

    for(int i=0; i<cnt; i++){

        long long int sum = 0;

        for(int j=0; j<q; j++){

            int aj = a[j]-1;

            int bj = b[j]-1;

            if(v[i][bj]-v[i][aj] == c[j]){

                sum += d[j];

            }

        }

        if(sum > max) max = sum;

    }

            

    cout << max << endl;

}

// Optimized solution to the same problem (faster):
//g++  5.4.0



#include <iostream>

#include <vector>

using namespace std;



int n, m, q;

int a[50],b[50],c[50],d[50];

long long int maxnum = 0;



long long int calc(std::vector<int>& v)

{

        long long int sum = 0;

        for(int j=0; j<q; j++){

            int aj = a[j]-1;

            int bj = b[j]-1;

            if(v[bj]-v[aj] == c[j]){

                sum += d[j];

            }

            //cout << a[j]-1 << "," << b[j]-1 << "," << c[j] << "," << d[j] << endl;

        }

        //cout << sum << endl;

        return sum;

}



void dfs(std::vector<int>& v)

{

    if(v.size() >= n){

        //for(int i=0; i<n; i++) cout << v[i];

        //cout << endl;

        long long int sum = calc(v);

        if(sum > maxnum) maxnum = sum;

        return;

    }

    

    int last = 1;

    if(v.size() > 0) last = v[v.size()-1];

    for(int i=last; i<=m; i++){

        v.push_back(i);

        dfs(v);

        v.pop_back();

    }

}



int main()

{

    cin >> n >> m >> q;

    for(int i=0; i<q; i++){

        cin >> a[i] >> b[i] >> c[i] >> d[i];

    }   

    std::vector<int> v;

    dfs(v);

    cout << maxnum << endl;

}

// Fastest known solution for this problem:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;



void DFS(int& ans, int m, int n, int q, vector<int>& A, const vector<int>& a, const vector<int>& b, const vector<int>& c, const vector<int>& d)

{

	if (A.size() == n) {

		int tmp = 0;

		for (int i = 0; i < q; i++) {

			if (A[b[i]] - A[a[i]] == c[i]) tmp += d[i];

		}

		ans = max(ans, tmp);

		return;

	}



	int start = A.empty() ? 1 : A.back();

	for (int i = start; i <= m; i++) {

		A.push_back(i);

		DFS(ans, m, n, q, A, a, b, c, d);

		A.pop_back();

	}

}



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];

		a[i]--, b[i]--;

	}



	int ans = 0;

	vector<int> A;

	DFS(ans, m, n, q, A, a, b, c, d);

	cout << ans << endl;

}