import sys

sys.setrecursionlimit(1000000000)

input = sys.stdin.readline

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

x=[list(map(int,input().split())) for i in range(q)]

for i in range(q):

    for j in range(2):

        x[i][j]-=1

a=[]

def dfs(v):

    if len(v)>n:

        return

    if len(v)==n:

        a.append(v)

    for i in range(int(v[-1]),m+1):

        dfs(v+[i])

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

    dfs([i])

ans=0

for i in a:

    now=0

    for k in range(q):

        if i[x[k][1]]-i[x[k][0]]==x[k][2]:

            now+=x[k][3]

    ans=max(now,ans)

print(ans)
from itertools import combinations_with_replacement

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

a,b,c,d=[0]*q,[0]*q,[0]*q,[0]*q

for i in range(q):

    x,y,z,w=list(map(int,input().split()))

    x-=1

    y-=1

    a[i],b[i],c[i],d[i]=x,y,z,w

ans=0

for v in combinations_with_replacement(list(range(1,m+1)),n):

    res=0

    for i in range(q):

        if v[b[i]]-v[a[i]]==c[i]:

            res+=d[i]

    ans=max(ans,res)

print(ans)
import sys

sys.setrecursionlimit(1000000000)
input = sys.stdin.readline
n, m, q = list(map(int, input().split()))
x = [list(map(int, input().split())) for i in range(q)]
for i in range(q):
    for j in range(2):
        x[i][j] -= 1
a = []


def dfs(v):
    if len(v) > n:
        return
    if len(v) == n:
        a.append(v)
    for i in range(int(v[-1]), m + 1):
        dfs(v + [i])


for i in range(1, m + 1):
    dfs([i])
ans = 0
for i in a:
    now = 0
    for k in range(q):
        if i[x[k][1]] - i[x[k][0]] == x[k][2]:
            now += x[k][3]
    ans = max(now, ans)
print(ans)

from itertools import combinations_with_replacement

n, m, q = list(map(int, input().split()))
a, b, c, d = [0] * q, [0] * q, [0] * q, [0] * q
for i in range(q):
    x, y, z, w = list(map(int, input().split()))
    x -= 1
    y -= 1
    a[i], b[i], c[i], d[i] = x, y, z, w
ans = 0
for v in combinations_with_replacement(list(range(1, m + 1)), n):
    res = 0
    for i in range(q):
        if v[b[i]] - v[a[i]] == c[i]:
            res += d[i]
    ans = max(ans, res)
print(ans)

-import sys +from itertools import combinations_with_replacement -sys.setrecursionlimit(1000000000) -input = sys.stdin.readline -x = [list(map(int, input().split())) for i in range(q)] +a, b, c, d = [0] * q, [0] * q, [0] * q, [0] * q -    for j in range(2): -        x[i][j] -= 1 -a = [] - - -def dfs(v): -    if len(v) > n: -        return -    if len(v) == n: -        a.append(v) -    for i in range(int(v[-1]), m + 1): -        dfs(v + [i]) - - -for i in range(1, m + 1): -    dfs([i]) +    x, y, z, w = list(map(int, input().split())) +    x -= 1 +    y -= 1 +    a[i], b[i], c[i], d[i] = x, y, z, w -for i in a: -    now = 0 -    for k in range(q): -        if i[x[k][1]] - i[x[k][0]] == x[k][2]: -            now += x[k][3] -    ans = max(now, ans) +for v in combinations_with_replacement(list(range(1, m + 1)), n): +    res = 0 +    for i in range(q): +        if v[b[i]] - v[a[i]] == c[i]: +            res += d[i] +    ans = max(ans, res)