import math

from collections import deque

n,m,q=[int(_) for _ in input().split()]

lista=[[int(i) for i in input().split()] for j in range(q)]

v=deque()  #0,0,0,0,0....,0

def make(a,b,c): #a=list b=maxlen c=max

    global v

    if len(a)==b:

        v.append(a)

    else:

        for i in range(a[-1],c+1):

            make(a+deque([i]),b,c)

for t in range(1,m+1):

    make(deque([t]),n,m)

max=0

for j in v:

    point=0

    for l in lista:

        if j[l[1]-1]-j[l[0]-1]==l[2]:

            point+=l[3]

    if max<point:

        max=point

print(max)
from collections import deque

n,m,q=[int(_) for _ in input().split()]

lista=[[int(i) for i in input().split()] for j in range(q)]

v=deque()  #0,0,0,0,0....,0

def make(a,b,c): #a=list b=maxlen c=max

    global v

    if len(a)==b:

        v.append(a)

    else:

        for i in range(a[-1],c+1):

            make(a+[i],b,c)

for t in range(1,m+1):

    make([t],n,m)

max=0

for j in v:

    point=0

    for l in lista:

        if j[l[1]-1]-j[l[0]-1]==l[2]:

            point+=l[3]

    if max<point:

        max=point

print(max)

import math
from collections import deque

n, m, q = [int(_) for _ in input().split()]
lista = [[int(i) for i in input().split()] for j in range(q)]
v = deque()  # 0,0,0,0,0....,0


def make(a, b, c):  # a=list b=maxlen c=max
    global v
    if len(a) == b:
        v.append(a)
    else:
        for i in range(a[-1], c + 1):
            make(a + deque([i]), b, c)


for t in range(1, m + 1):
    make(deque([t]), n, m)
max = 0
for j in v:
    point = 0
    for l in lista:
        if j[l[1] - 1] - j[l[0] - 1] == l[2]:
            point += l[3]
    if max < point:
        max = point
print(max)

from collections import deque

n, m, q = [int(_) for _ in input().split()]
lista = [[int(i) for i in input().split()] for j in range(q)]
v = deque()  # 0,0,0,0,0....,0


def make(a, b, c):  # a=list b=maxlen c=max
    global v
    if len(a) == b:
        v.append(a)
    else:
        for i in range(a[-1], c + 1):
            make(a + [i], b, c)


for t in range(1, m + 1):
    make([t], n, m)
max = 0
for j in v:
    point = 0
    for l in lista:
        if j[l[1] - 1] - j[l[0] - 1] == l[2]:
            point += l[3]
    if max < point:
        max = point
print(max)

-import math -            make(a + deque([i]), b, c) +            make(a + [i], b, c) -    make(deque([t]), n, m) +    make([t], n, m)