import itertools

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

Se=[]

for i in range(q):

    Se.append(tuple(map(int,input().split())))

#print(Se)

integerset={i for i in range(1,m+1)}

#print(integerset)

everyset=itertools.combinations_with_replacement(integerset,n)

max=0

for v in everyset:

  #  print(v)

 #   print(len(v))

    sum=0

    for i in range(len(Se)):

        if v[Se[i][1]-1]-v[Se[i][0]-1]==Se[i][2]:

            sum+=Se[i][3]

   # print(sum)

    if sum>max:

        max=sum

print(max)

import itertools

def main():

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

    Se=[]

    for i in range(q):

        Se.append(tuple(map(int,input().split())))

    #print(Se)

    integerset={i for i in range(1,m+1)}

    #print(integerset)

    everyset=itertools.combinations_with_replacement(integerset,n)

    max=0

    for v in everyset:

    #  print(v)

    #   print(len(v))

        sum=0

        for i in range(len(Se)):

            if v[Se[i][1]-1]-v[Se[i][0]-1]==Se[i][2]:

                sum+=Se[i][3]

    # print(sum)

        if sum>max:

            max=sum

    print(max)

if __name__=="__main__":

    main()
import itertools

n, m, q = list(map(int, input().split()))
Se = []
for i in range(q):
    Se.append(tuple(map(int, input().split())))
# print(Se)
integerset = {i for i in range(1, m + 1)}
# print(integerset)
everyset = itertools.combinations_with_replacement(integerset, n)
max = 0
for v in everyset:
    #  print(v)
    #   print(len(v))
    sum = 0
    for i in range(len(Se)):
        if v[Se[i][1] - 1] - v[Se[i][0] - 1] == Se[i][2]:
            sum += Se[i][3]
    # print(sum)
    if sum > max:
        max = sum
print(max)

import itertools


def main():
    n, m, q = list(map(int, input().split()))
    Se = []
    for i in range(q):
        Se.append(tuple(map(int, input().split())))
    # print(Se)
    integerset = {i for i in range(1, m + 1)}
    # print(integerset)
    everyset = itertools.combinations_with_replacement(integerset, n)
    max = 0
    for v in everyset:
        #  print(v)
        #   print(len(v))
        sum = 0
        for i in range(len(Se)):
            if v[Se[i][1] - 1] - v[Se[i][0] - 1] == Se[i][2]:
                sum += Se[i][3]
        # print(sum)
        if sum > max:
            max = sum
    print(max)


if __name__ == "__main__":
    main()

-n, m, q = list(map(int, input().split())) -Se = [] -for i in range(q): -    Se.append(tuple(map(int, input().split()))) -# print(Se) -integerset = {i for i in range(1, m + 1)} -# print(integerset) -everyset = itertools.combinations_with_replacement(integerset, n) -max = 0 -for v in everyset: -    #  print(v) -    #   print(len(v)) -    sum = 0 -    for i in range(len(Se)): -        if v[Se[i][1] - 1] - v[Se[i][0] - 1] == Se[i][2]: -            sum += Se[i][3] -    # print(sum) -    if sum > max: -        max = sum -print(max) + +def main(): +    n, m, q = list(map(int, input().split())) +    Se = [] +    for i in range(q): +        Se.append(tuple(map(int, input().split()))) +    # print(Se) +    integerset = {i for i in range(1, m + 1)} +    # print(integerset) +    everyset = itertools.combinations_with_replacement(integerset, n) +    max = 0 +    for v in everyset: +        #  print(v) +        #   print(len(v)) +        sum = 0 +        for i in range(len(Se)): +            if v[Se[i][1] - 1] - v[Se[i][0] - 1] == Se[i][2]: +                sum += Se[i][3] +        # print(sum) +        if sum > max: +            max = sum +    print(max) + + +if __name__ == "__main__": +    main()