from sys import stdin

from collections import deque

def main():

    #入力

    readline=stdin.readline

    N,M,Q=list(map(int,readline().split()))

    a=[]

    b=[]

    c=[]

    d=[]

    for i in range(Q):

        li=list(map(int,readline().split()))

        a.append(li[0])

        b.append(li[1])

        c.append(li[2])

        d.append(li[3])



    As=[]

    de=[]

    for i in range(1,M+1):

        de.append([i])

    de=deque(de)

    while len(de)>=1:

        x=de.popleft()

        if len(x)==N:

            As.append(x)

        else:

            n=x[-1]

            for i in range(n,M+1):

                y=x[:]

                y.append(i)

                de.append(y)



    max_res=0

    for i in range(len(As)):

        A=As[i]

        res=0

        for j in range(Q):

            if A[b[j]-1]-A[a[j]-1]==c[j]:

                res+=d[j]

        max_res=max(max_res,res)

    print(max_res)

if __name__=="__main__":

    main()
from sys import stdin

import itertools

def main():

    #入力

    readline=stdin.readline

    N,M,Q=list(map(int,readline().split()))

    a=[0]*Q

    b=[0]*Q

    c=[0]*Q

    d=[0]*Q

    for i in range(Q):

        a[i],b[i],c[i],d[i]=list(map(int,readline().split()))



    arrays=list(itertools.combinations_with_replacement(list(range(1,M+1)),N))

    max_res=0

    for A in arrays:

        res=0

        for i in range(Q):

            if A[b[i]-1]-A[a[i]-1]==c[i]:

                res+=d[i]

        max_res=max(max_res,res)

    print(max_res)

if __name__=="__main__":

    main()
from sys import stdin
from collections import deque


def main():
    # 入力
    readline = stdin.readline
    N, M, Q = list(map(int, readline().split()))
    a = []
    b = []
    c = []
    d = []
    for i in range(Q):
        li = list(map(int, readline().split()))
        a.append(li[0])
        b.append(li[1])
        c.append(li[2])
        d.append(li[3])
    As = []
    de = []
    for i in range(1, M + 1):
        de.append([i])
    de = deque(de)
    while len(de) >= 1:
        x = de.popleft()
        if len(x) == N:
            As.append(x)
        else:
            n = x[-1]
            for i in range(n, M + 1):
                y = x[:]
                y.append(i)
                de.append(y)
    max_res = 0
    for i in range(len(As)):
        A = As[i]
        res = 0
        for j in range(Q):
            if A[b[j] - 1] - A[a[j] - 1] == c[j]:
                res += d[j]
        max_res = max(max_res, res)
    print(max_res)


if __name__ == "__main__":
    main()

from sys import stdin
import itertools


def main():
    # 入力
    readline = stdin.readline
    N, M, Q = list(map(int, readline().split()))
    a = [0] * Q
    b = [0] * Q
    c = [0] * Q
    d = [0] * Q
    for i in range(Q):
        a[i], b[i], c[i], d[i] = list(map(int, readline().split()))
    arrays = list(itertools.combinations_with_replacement(list(range(1, M + 1)), N))
    max_res = 0
    for A in arrays:
        res = 0
        for i in range(Q):
            if A[b[i] - 1] - A[a[i] - 1] == c[i]:
                res += d[i]
        max_res = max(max_res, res)
    print(max_res)


if __name__ == "__main__":
    main()

-from collections import deque +import itertools -    a = [] -    b = [] -    c = [] -    d = [] +    a = [0] * Q +    b = [0] * Q +    c = [0] * Q +    d = [0] * Q -        li = list(map(int, readline().split())) -        a.append(li[0]) -        b.append(li[1]) -        c.append(li[2]) -        d.append(li[3]) -    As = [] -    de = [] -    for i in range(1, M + 1): -        de.append([i]) -    de = deque(de) -    while len(de) >= 1: -        x = de.popleft() -        if len(x) == N: -            As.append(x) -        else: -            n = x[-1] -            for i in range(n, M + 1): -                y = x[:] -                y.append(i) -                de.append(y) +        a[i], b[i], c[i], d[i] = list(map(int, readline().split())) +    arrays = list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)) -    for i in range(len(As)): -        A = As[i] +    for A in arrays: -        for j in range(Q): -            if A[b[j] - 1] - A[a[j] - 1] == c[j]: -                res += d[j] +        for i in range(Q): +            if A[b[i] - 1] - A[a[i] - 1] == c[i]: +                res += d[i]