unittest
{
	assert( [ "3 4 3", "1 3 3 100", "1 2 2 10", "2 3 2 10" ].parse.expand.solve == 110 );
	assert( [ "4 6 10", "2 4 1 86568", "1 4 0 90629", "2 3 0 90310", "3 4 1 29211", "3 4 3 78537", "3 4 2 8580", "1 2 1 96263", "1 4 2 2156", "1 2 0 94325", "1 4 3 94328" ].parse.expand.solve == 357500 );
	assert( [ "10 10 1", "1 10 9 1" ].parse.expand.solve == 1 );
}



import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto nmq = input.front.split.to!( long[] );
	auto abcds = new long[ 4 ][ nmq[ 2 ] ];
	foreach( ref abcd; abcds )
	{
		input.popFront;
		abcd = input.front.split.to!( long[] );
		abcd[ 0 .. 2 ] -= 1;
	}
	return tuple( nmq[ 0 ], nmq[ 1 ], abcds );
}

auto solve( long n, long m, long[ 4 ][] abcds )
{
	return solve2( abcds, 1, m, new long[ n ], 0 );
}

long solve2( long[ 4 ][] abcds, long mi, long ma, long[] ds, long i )
{
	auto s = 0L;
	if( i < ds.length )
	{
		foreach( d; mi .. ma + 1 )
		{
			ds[ i ] = d;
			s = max( s, solve2( abcds, d, ma, ds, i + 1 ) );
		}
	}
	else
	{
		foreach( abcd; abcds )
		{
			if( ds[ abcd[ 1 ] ] - ds[ abcd[ 0 ] ] == abcd[ 2 ] ) s += abcd[ 3 ];
		}
	}
	return s;
}


