import sys

## io ##

def IS(): return sys.stdin.readline().rstrip()

def II(): return int(IS())

def MII(): return list(map(int, IS().split()))

def MIIZ(): return list([x-1 for x in MII()])

## dp ##

def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]

def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]

## math ##

def to_bin(x: int) -> str: return format(x, 'b') # rev => int(res, 2)

def to_oct(x: int) -> str: return format(x, 'o') # rev => int(res, 8)

def to_hex(x: int) -> str: return format(x, 'x') # rev => int(res, 16)

MOD=10**9+7

def divc(x,y) -> int: return -(-x//y)

def divf(x,y) -> int: return x//y

def gcd(x,y):

    while y: x,y = y,x%y

    return x

def lcm(x,y): return x*y//gcd(x,y)

def enumerate_divs(n):

    """Return a tuple list of divisor of n"""

    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]

def get_primes(MAX_NUM=10**3):

    """Return a list of prime numbers n or less"""

    is_prime = [True]*(MAX_NUM+1)

    is_prime[0] = is_prime[1] = False

    for i in range(2, int(MAX_NUM**0.5)+1):

        if not is_prime[i]: continue

        for j in range(i*2, MAX_NUM+1, i):

            is_prime[j] = False

    return [i for i in range(MAX_NUM+1) if is_prime[i]]

## libs ##

from itertools import accumulate as acc

from itertools import combinations_with_replacement as combi

from collections import deque, Counter

from heapq import heapify, heappop, heappush

from bisect import bisect_left

#======================================================#



def main():

    n, m, q = MII()

    query = [MII() for _ in range(q)]

    # l = [i for i in range(1,m+1)]

    # maxv = 0

    # for v in combi(l,n):

    #     sumv = 0

    #     for a,b,c,d in query:

    #         if v[b-1]-v[a-1] == c:

    #             sumv += d

    #     maxv = max(maxv, sumv)

    # print(maxv)

    def score(A):

        res = 0

        for a, b, c, d in query:

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

                res += d

        return res



    def dfs(A):

        if len(A) == n:

            return score(A)

        res = 0

        prev_last = A[-1] if len(A) > 0 else 0

        for v in range(prev_last, m):

            A.append(v)

            res = max(res, dfs(A))

            A.pop()

        return res

    print((dfs([])))





if __name__ == '__main__':

    main()
import sys

## io ##

def IS(): return sys.stdin.readline().rstrip()

def II(): return int(IS())

def MII(): return list(map(int, IS().split()))

#======================================================#



def main():

    n, m, q = MII()

    query = [MII() for _ in range(q)]



    def calc_point(lst):

        sumv = 0

        for a, b, c, d in query:

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

                sumv += d

        return sumv



    def dfs(lst):

        if len(lst) == n:

            return calc_point(lst)

        point = 0

        last_elm = lst[-1]

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

            point = max(point, dfs(lst+[i]))

        return point



    print((dfs([1])))





if __name__ == '__main__':

    main()
import sys

## io ##
def IS():
    return sys.stdin.readline().rstrip()


def II():
    return int(IS())


def MII():
    return list(map(int, IS().split()))


def MIIZ():
    return list([x - 1 for x in MII()])


## dp ##
def DD2(d1, d2, init=0):
    return [[init] * d2 for _ in range(d1)]


def DD3(d1, d2, d3, init=0):
    return [DD2(d2, d3, init) for _ in range(d1)]


## math ##
def to_bin(x: int) -> str:
    return format(x, "b")  # rev => int(res, 2)


def to_oct(x: int) -> str:
    return format(x, "o")  # rev => int(res, 8)


def to_hex(x: int) -> str:
    return format(x, "x")  # rev => int(res, 16)


MOD = 10**9 + 7


def divc(x, y) -> int:
    return -(-x // y)


def divf(x, y) -> int:
    return x // y


def gcd(x, y):
    while y:
        x, y = y, x % y
    return x


def lcm(x, y):
    return x * y // gcd(x, y)


def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i, n // i) for i in range(1, int(n**0.5) + 1) if n % i == 0]


def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True] * (MAX_NUM + 1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, MAX_NUM + 1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM + 1) if is_prime[i]]


## libs ##
from itertools import accumulate as acc
from itertools import combinations_with_replacement as combi
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left

# ======================================================#
def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]
    # l = [i for i in range(1,m+1)]
    # maxv = 0
    # for v in combi(l,n):
    #     sumv = 0
    #     for a,b,c,d in query:
    #         if v[b-1]-v[a-1] == c:
    #             sumv += d
    #     maxv = max(maxv, sumv)
    # print(maxv)
    def score(A):
        res = 0
        for a, b, c, d in query:
            if A[b - 1] - A[a - 1] == c:
                res += d
        return res

    def dfs(A):
        if len(A) == n:
            return score(A)
        res = 0
        prev_last = A[-1] if len(A) > 0 else 0
        for v in range(prev_last, m):
            A.append(v)
            res = max(res, dfs(A))
            A.pop()
        return res

    print((dfs([])))


if __name__ == "__main__":
    main()

import sys

## io ##
def IS():
    return sys.stdin.readline().rstrip()


def II():
    return int(IS())


def MII():
    return list(map(int, IS().split()))


# ======================================================#
def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]

    def calc_point(lst):
        sumv = 0
        for a, b, c, d in query:
            if lst[b - 1] - lst[a - 1] == c:
                sumv += d
        return sumv

    def dfs(lst):
        if len(lst) == n:
            return calc_point(lst)
        point = 0
        last_elm = lst[-1]
        for i in range(last_elm, m + 1):
            point = max(point, dfs(lst + [i]))
        return point

    print((dfs([1])))


if __name__ == "__main__":
    main()

-def MIIZ(): -    return list([x - 1 for x in MII()]) - - -## dp ## -def DD2(d1, d2, init=0): -    return [[init] * d2 for _ in range(d1)] - - -def DD3(d1, d2, d3, init=0): -    return [DD2(d2, d3, init) for _ in range(d1)] - - -## math ## -def to_bin(x: int) -> str: -    return format(x, "b")  # rev => int(res, 2) - - -def to_oct(x: int) -> str: -    return format(x, "o")  # rev => int(res, 8) - - -def to_hex(x: int) -> str: -    return format(x, "x")  # rev => int(res, 16) - - -MOD = 10**9 + 7 - - -def divc(x, y) -> int: -    return -(-x // y) - - -def divf(x, y) -> int: -    return x // y - - -def gcd(x, y): -    while y: -        x, y = y, x % y -    return x - - -def lcm(x, y): -    return x * y // gcd(x, y) - - -def enumerate_divs(n): -    """Return a tuple list of divisor of n""" -    return [(i, n // i) for i in range(1, int(n**0.5) + 1) if n % i == 0] - - -def get_primes(MAX_NUM=10**3): -    """Return a list of prime numbers n or less""" -    is_prime = [True] * (MAX_NUM + 1) -    is_prime[0] = is_prime[1] = False -    for i in range(2, int(MAX_NUM**0.5) + 1): -        if not is_prime[i]: -            continue -        for j in range(i * 2, MAX_NUM + 1, i): -            is_prime[j] = False -    return [i for i in range(MAX_NUM + 1) if is_prime[i]] - - -## libs ## -from itertools import accumulate as acc -from itertools import combinations_with_replacement as combi -from collections import deque, Counter -from heapq import heapify, heappop, heappush -from bisect import bisect_left - -    # l = [i for i in range(1,m+1)] -    # maxv = 0 -    # for v in combi(l,n): -    #     sumv = 0 -    #     for a,b,c,d in query: -    #         if v[b-1]-v[a-1] == c: -    #             sumv += d -    #     maxv = max(maxv, sumv) -    # print(maxv) -    def score(A): -        res = 0 + +    def calc_point(lst): +        sumv = 0 -            if A[b - 1] - A[a - 1] == c: -                res += d -        return res +            if lst[b - 1] - lst[a - 1] == c: +                sumv += d +        return sumv -    def dfs(A): -        if len(A) == n: -            return score(A) -        res = 0 -        prev_last = A[-1] if len(A) > 0 else 0 -        for v in range(prev_last, m): -            A.append(v) -            res = max(res, dfs(A)) -            A.pop() -        return res +    def dfs(lst): +        if len(lst) == n: +            return calc_point(lst) +        point = 0 +        last_elm = lst[-1] +        for i in range(last_elm, m + 1): +            point = max(point, dfs(lst + [i])) +        return point -    print((dfs([]))) +    print((dfs([1])))