// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



int N, M, Q;

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



int main() {

    cin >> N >> M >> Q;

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

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

    }

    int ma = 0;

    for (int i0 = 1; 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) {

                                            vector<int> A{i0, i1, i2, i3, i4, i5, i6, i7, i8, i9};

                                            int point = 0;

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

                                                if (A[b[i]-1] - A[a[i]-1] == c[i]) {

                                                    point += d[i];

                                                }

                                            }

                                            if (ma < point) {

                                                ma = point;

                                            }

                                        }

                                    }

                                }

                            }

                        }

                    }

                }

            }

        }

    }

    cout << ma << "\n";

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



int N, M, Q;

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

int A[10];

int max_score = 0;



void dfs(int dep, int low) {

    if (dep == N) {

        int score = 0;

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

            if (A[b[i]-1] - A[a[i]-1] == c[i]) {

                score += d[i];

            }

        }

        max_score = max(max_score, score);

    }

    else {

        for (int i = low; i <= M; ++i) {

            A[dep] = i;

            dfs(dep + 1, i);

        }

    }

}



int main() {

    cin >> N >> M >> Q;

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

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

    }



    dfs(0, 1);

    cout << max_score << "\n";

}

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

}