import sys

read = sys.stdin.read

readlines = sys.stdin.readlines

from itertools import combinations_with_replacement

def main():

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

    qs = []

    for _ in range(q):

        abcd = tuple(map(int, input().split()))

        qs.append(abcd)

    A = tuple(combinations_with_replacement(list(range(1, m+1)), n))

    r = 0

    for Ae in A:

        t0 = 0

        for qe in qs:

            if Ae[qe[1]-1] - Ae[qe[0]-1] == qe[2]:

                t0 += qe[3]

        r = max(r, t0)

    print(r)



if __name__ == '__main__':

    main()

import sys

read = sys.stdin.read

readlines = sys.stdin.readlines

from itertools import combinations_with_replacement

import numpy as np

def main():

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

    A = np.array(list(combinations_with_replacement(list(range(1, m + 1)), n)))

    numA = len(A)

    score = np.zeros(numA, np.int32)

    ma = list(map(int, read().split()))

    for a, b, c, d in zip(ma, ma, ma, ma):

        a -= 1

        b -= 1

        eachA_is_equalOrNot = A[:, b] - A[:, a] == c

        score += d * eachA_is_equalOrNot

        # print(score)

    print((score.max()))



if __name__ == '__main__':

    main()
import sys

read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import combinations_with_replacement


def main():
    n, m, q = list(map(int, input().split()))
    qs = []
    for _ in range(q):
        abcd = tuple(map(int, input().split()))
        qs.append(abcd)
    A = tuple(combinations_with_replacement(list(range(1, m + 1)), n))
    r = 0
    for Ae in A:
        t0 = 0
        for qe in qs:
            if Ae[qe[1] - 1] - Ae[qe[0] - 1] == qe[2]:
                t0 += qe[3]
        r = max(r, t0)
    print(r)


if __name__ == "__main__":
    main()

import sys

read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import combinations_with_replacement
import numpy as np


def main():
    n, m, q = list(map(int, input().split()))
    A = np.array(list(combinations_with_replacement(list(range(1, m + 1)), n)))
    numA = len(A)
    score = np.zeros(numA, np.int32)
    ma = list(map(int, read().split()))
    for a, b, c, d in zip(ma, ma, ma, ma):
        a -= 1
        b -= 1
        eachA_is_equalOrNot = A[:, b] - A[:, a] == c
        score += d * eachA_is_equalOrNot
        # print(score)
    print((score.max()))


if __name__ == "__main__":
    main()

+import numpy as np -    qs = [] -    for _ in range(q): -        abcd = tuple(map(int, input().split())) -        qs.append(abcd) -    A = tuple(combinations_with_replacement(list(range(1, m + 1)), n)) -    r = 0 -    for Ae in A: -        t0 = 0 -        for qe in qs: -            if Ae[qe[1] - 1] - Ae[qe[0] - 1] == qe[2]: -                t0 += qe[3] -        r = max(r, t0) -    print(r) +    A = np.array(list(combinations_with_replacement(list(range(1, m + 1)), n))) +    numA = len(A) +    score = np.zeros(numA, np.int32) +    ma = list(map(int, read().split())) +    for a, b, c, d in zip(ma, ma, ma, ma): +        a -= 1 +        b -= 1 +        eachA_is_equalOrNot = A[:, b] - A[:, a] == c +        score += d * eachA_is_equalOrNot +        # print(score) +    print((score.max()))