#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define ll long long int

int main(void)
{
	int N, M, Q;
	scanf("%d %d %d", &N, &M, &Q);
	int a[Q+1];
	int b[Q+1];
	int c[Q+1];
	int d[Q+1];
	int A[N + 1];

	memset(A, 0, sizeof(A[0]) * (N + 1));

	for (int i = 1; i <= Q; i++)
	{
		scanf("%d %d %d %d", &a[i], &b[i], &c[i], &d[i]);
	}

	int max = 0;
    int score=0;
	int idx=1;
	for (int j = 1; j <= M; j++)
	{
		A[1] = j;

		for (int i = 1; i <= Q; i++)
		{
			if (A[b[i]] == 0)
			{
				A[b[i]] = c[i] + A[a[i]];
				if (A[b[i]]>M)A[b[i]]=-1;
			}
			if (A[a[i]] == 0)
			{
				A[a[i]] = A[b[i]] - c[i];
				if (A[a[i]]>=M)A[a[i]]=-1;
			}
		}
		for (int i = 1; i <= Q; i++)
		{

			idx++;
			if (A[b[i]] - A[a[i]] == c[i])
			{
				score += d[i];
			}
		}
		memset(A, 0, sizeof(A[0]) * (N + 1));
		if (score>max)max=score;
		score=0;
		idx=1;
	}


	printf("%d", max);

	return 0;
}
