import sys

from io import StringIO

import unittest



sys.setrecursionlimit(10**5)



def search(seq, min_n, max_n, n, req):

	ans = 0

	if len(seq) == n:

		for a, b, c, d in req:

			if seq[b-1] - seq[a-1] == c:

				ans += d

		return ans

	else:

		for i in range(min_n, max_n+1):

			new_seq = seq + [i]

			ans = max(ans, search(new_seq, i, max_n, n, req))

	return ans



def resolve():

	n, m, q = list(map(int, input().split()))

	req = [list(map(int, input().split())) for _ in range(q)]

	print((search([], 1, m, n, req)))

resolve()
def search(v, n, m, seq, scs):

    if len(seq) == n:

        ans = 0

        for r in scs:

            if seq[r[1]-1] - seq[r[0]-1] == r[2]:

                ans += r[3]

        return ans

    ans = 0

    for i in range(v, m+1):

        tmp = search(i, n, m, seq + [i], scs)

        ans = max(ans, tmp)

    return ans



def resolve():

    n, m, q = list(map(int, input().split()))

    scs = [list(map(int, input().split())) for _ in range(q)]

    v = search(1, n, m, [], scs)

    print(v)

resolve()
import sys
from io import StringIO
import unittest

sys.setrecursionlimit(10**5)


def search(seq, min_n, max_n, n, req):
    ans = 0
    if len(seq) == n:
        for a, b, c, d in req:
            if seq[b - 1] - seq[a - 1] == c:
                ans += d
        return ans
    else:
        for i in range(min_n, max_n + 1):
            new_seq = seq + [i]
            ans = max(ans, search(new_seq, i, max_n, n, req))
    return ans


def resolve():
    n, m, q = list(map(int, input().split()))
    req = [list(map(int, input().split())) for _ in range(q)]
    print((search([], 1, m, n, req)))


resolve()

def search(v, n, m, seq, scs):
    if len(seq) == n:
        ans = 0
        for r in scs:
            if seq[r[1] - 1] - seq[r[0] - 1] == r[2]:
                ans += r[3]
        return ans
    ans = 0
    for i in range(v, m + 1):
        tmp = search(i, n, m, seq + [i], scs)
        ans = max(ans, tmp)
    return ans


def resolve():
    n, m, q = list(map(int, input().split()))
    scs = [list(map(int, input().split())) for _ in range(q)]
    v = search(1, n, m, [], scs)
    print(v)


resolve()

-import sys -from io import StringIO -import unittest - -sys.setrecursionlimit(10**5) - - -def search(seq, min_n, max_n, n, req): +def search(v, n, m, seq, scs): +    if len(seq) == n: +        ans = 0 +        for r in scs: +            if seq[r[1] - 1] - seq[r[0] - 1] == r[2]: +                ans += r[3] +        return ans -    if len(seq) == n: -        for a, b, c, d in req: -            if seq[b - 1] - seq[a - 1] == c: -                ans += d -        return ans -    else: -        for i in range(min_n, max_n + 1): -            new_seq = seq + [i] -            ans = max(ans, search(new_seq, i, max_n, n, req)) +    for i in range(v, m + 1): +        tmp = search(i, n, m, seq + [i], scs) +        ans = max(ans, tmp) -    req = [list(map(int, input().split())) for _ in range(q)] -    print((search([], 1, m, n, req))) +    scs = [list(map(int, input().split())) for _ in range(q)] +    v = search(1, n, m, [], scs) +    print(v)