from sys import stdin

import itertools

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

q=[]

num_list=[]

number=[]

score=[0]

for i in range(1,M+1):number.append(i)

for _ in range(Q):

    a,b,c,d=list(map(int, (stdin.readline().strip().split())))

    q.append([a,b,c,d])

def dfs(num_list,i):

    if i==M and len(num_list)<N:

        return

    if len(num_list)!=N:

        a=num_list.copy()

        num_list.append(number[i])

        dfs(num_list,i)

        dfs(num_list,i+1)

        dfs(a,i+1)

    else:

        ans=0

        for a,b,c,d in q:

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

                ans+=d

        score[0]=max(ans,score[0])







dfs(num_list,0)

print((score[0]))

from sys import stdin

import itertools

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

q=[]

num_list=[]

number=[]

score=[0]

daburi=[]

for i in range(1,M+1):number.append(i)

for _ in range(Q):

    a,b,c,d=list(map(int, (stdin.readline().strip().split())))

    q.append([a,b,c,d])

def dfs(num_list,i):

    if i==M and len(num_list)<N:

        return

    if len(num_list)!=N:

        a=num_list.copy()

        num_list.append(number[i])

        dfs(num_list,i)

        dfs(a,i+1)

    else:

        ans=0

        for a,b,c,d in q:

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

                ans+=d

        score[0]=max(ans,score[0])







dfs(num_list,0)

print((score[0]))

from sys import stdin
import itertools

N, M, Q = list(map(int, (stdin.readline().strip().split())))
q = []
num_list = []
number = []
score = [0]
for i in range(1, M + 1):
    number.append(i)
for _ in range(Q):
    a, b, c, d = list(map(int, (stdin.readline().strip().split())))
    q.append([a, b, c, d])


def dfs(num_list, i):
    if i == M and len(num_list) < N:
        return
    if len(num_list) != N:
        a = num_list.copy()
        num_list.append(number[i])
        dfs(num_list, i)
        dfs(num_list, i + 1)
        dfs(a, i + 1)
    else:
        ans = 0
        for a, b, c, d in q:
            if num_list[b - 1] - num_list[a - 1] == c:
                ans += d
        score[0] = max(ans, score[0])


dfs(num_list, 0)
print((score[0]))

from sys import stdin
import itertools

N, M, Q = list(map(int, (stdin.readline().strip().split())))
q = []
num_list = []
number = []
score = [0]
daburi = []
for i in range(1, M + 1):
    number.append(i)
for _ in range(Q):
    a, b, c, d = list(map(int, (stdin.readline().strip().split())))
    q.append([a, b, c, d])


def dfs(num_list, i):
    if i == M and len(num_list) < N:
        return
    if len(num_list) != N:
        a = num_list.copy()
        num_list.append(number[i])
        dfs(num_list, i)
        dfs(a, i + 1)
    else:
        ans = 0
        for a, b, c, d in q:
            if num_list[b - 1] - num_list[a - 1] == c:
                ans += d
        score[0] = max(ans, score[0])


dfs(num_list, 0)
print((score[0]))

+daburi = [] -        dfs(num_list, i + 1)