CodeEff/ECCO huggingface dataset:
# Original solution
from pprint import pprint
 
n,m,q = list(map(int, input().split()))
 
ql = [list(map(int,input().split())) for _ in range(q)]
 
#print(n)
#print(m)
#print(q)
#pprint(ls)
 
masterlist = []
minilist = [0] * n
i = 0
 
 
def saiki(before_a,i,minilist):
    if i > n-1:
        masterlist.append([minilist[j] for j in range(n)])
        return
    for Ai in range(before_a,m+1):
        minilist[i] = Ai
        saiki(Ai,i+1,minilist)
 
#for before_a in range(1,m+1):
#    saiki(before_a,where,minilist)
 
before_a = 1
saiki(before_a,i,minilist)
 
#pprint(masterlist)
#pprint(ql)
 
dmax = 0
 
for a_mini in masterlist:
    dtemp = 0
    for ql_mini in ql:
        if a_mini[ql_mini[1]-1] - a_mini[ql_mini[0]-1] == ql_mini[2]:
            dtemp += ql_mini[3]
        else:
            pass
    if dtemp >= dmax:
        dmax = dtemp
 
print(dmax)

# Optimized solution
from pprint import pprint
import itertools


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

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



a_lists = list(itertools.combinations_with_replacement(list(range(m)), n))

#print(a_lists)

dmax = 0

for a_mini in a_lists:
    dtemp = 0
    for ql_mini in ql:
        if a_mini[ql_mini[1]-1] - a_mini[ql_mini[0]-1] == ql_mini[2]:
            dtemp += ql_mini[3]
        else:
            pass
    if dtemp >= dmax:
        dmax = dtemp

print(dmax)
# Original solution
import itertools
n,m,q = list(map(int, input().split()))
abc = []
d = []
for _ in range(q):
    A,B,C,D = list(map(int, input().split()))
    abc.append([A,B,C])
    d.append(D)

l = list(range(1,m+1))
point = 0
for v in itertools.combinations_with_replacement(l,n):
    p = 0
    for i in range(n-1):
        for j in range(i+1, n):
            if [i+1,j+1, abs(v[j]-v[i])] in abc:
                p += d[abc.index([i+1,j+1, abs(v[j]-v[i])])]
    if point < p: point = p
print(point)

# Optimized solution
import itertools
n,m,q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for _ in range(q)]

l = list(range(1,m+1))
point = 0
for v in itertools.combinations_with_replacement(l,n):
    p = 0
    for a,b,c,d in abcd:
        if v[b-1] - v[a-1] == c:
            p += d
    if p > point: point = p
print(point)
# Original solution
n,m,q = list(map(int,input().split()))
from itertools import combinations_with_replacement

a,b,c,d = [],[],[],[]
for i in range(q):
    e,f,g,h = list(map(int,input().split()))
    a.append(e-1)
    b.append(f-1)
    c.append(g)
    d.append(h)

ans = 0
N = [i+1 for i in range(m)]

for v in combinations_with_replacement(N,n):
    num = 0
    #print(v)
    for i in range(q):
        if v[b[i]] - v[a[i]] == c[i]:
            num += d[i]
    ans = max(ans,num)
print(ans)

# Optimized solution
from itertools import combinations_with_replacement

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

L = [i+1 for i in range(m)]
li = [input().split() for i in range(q)]

for i in range(q):
    li[i] = [int(x) for x in li[i]]

ans = 0
for v in combinations_with_replacement(L,n):
    sum1 = 0
    for i in li:
        if v[i[1]-1] - v[i[0]-1] == i[2]:
            sum1 +=i[3]
    ans = max(ans,sum1)
print(ans)
# Original solution
from collections import deque


def calc(nums):
    ret = 0
    for a, b, c, d in req:
        if nums[b - 1] - nums[a - 1] == c:
            ret += d

    return ret


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

ans = 0

que = deque()

# [1]～[m]までキューに追加します。実は[1]だけでもいいです。考えてみてください。
for i in range(1, m + 1):
    que.append([i])

while que:
    seq = que.pop()

    if len(seq) == n:
        score = calc(seq)
        ans = max(ans, score)
    else:
        for i in range(seq[-1], m + 1):
            seq_next = seq + [i]
            que.append(seq_next)

print(ans)


# Optimized solution

def calc(nums):
    ret = 0
    for a, b, c, d in req:
        if nums[b - 1] - nums[a - 1] == c:
            ret += d
 
    return ret
 
 
n, m, q = list(map(int, input().split()))
req = [list(map(int, input().split())) for _ in range(q)]
 
ans = 0
 
stack = []
 
# [1]～[m]までキューに追加します。実は[1]だけでもいいです。考えてみてください。
for i in range(1, m + 1):
    stack.append([i])
 
while stack:
    seq = stack.pop()
 
    if len(seq) == n:
        score = calc(seq)
        ans = max(ans, score)
    else:
        for i in range(seq[-1], m + 1):
            seq_next = seq + [i]
            stack.append(seq_next)
 
print(ans)
# Original solution
from collections import deque


# 数列の点数を計算する関数
def calc(seq):
    score = 0
    for a, b, c, d in req:
        if seq[b - 1] - seq[a - 1] == c:
            score += d

    return score


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

ans = 0

que = deque()

# 数列の1番目、[1]～[m]までキューに追加しますが、
# 実は、この問題は数列の最初が[1]の場合だけを考えても解けます。
for i in range(1, m + 1):
    que.append([i])

while que:
    seq = que.popleft()

    if len(seq) == n:
        # 長さがnになったので、得点を計算します
        score = calc(seq)
        ans = max(ans, score)
    else:
        # 次に追加する数字は、下限が今の数列の一番後ろの数字、上限がmです
        for i in range(seq[-1], m + 1):
            seq_next = seq + [i]
            que.append(seq_next)

print(ans)


# Optimized solution
def dfs(seq):
    # 返り値は、すべての数列の得点の最大値 ans です。
    ans = 0
    if len(seq) == n:
        # 数列が完成したので、得点を計算します
        score_ret = 0
        for a, b, c, d in req:
            if seq[b-1] - seq[a-1] == c:
                score_ret += d
        return score_ret  # この数列の得点を返します
    else:
        # まだ数列が完成していません
        for i in range(seq[-1], m + 1):
            seq_next = seq + (i,)  # 長さ1のタプル(i,)を連結します
            score = dfs(seq_next)  # seq_nextから派生するすベての数列の中での、得点の最大値が返ってきます
            ans = max(ans, score)  # 最大の得点を更新します

    # すべてが終わったので、得点の最大値を返します
    return ans


n, m, q = list(map(int, input().split()))
# reqは[[a1,b1,c1,d1],[a2,b2,c2,d2]……]が入ったリストのリストです
req = [list(map(int, input().split())) for _ in range(q)]

# 最終的に答えが返ってくるようにします。処理はすべてdfsメソッドでやってもらいます。
# リストだとどこかで間違えて値を書き換えそうで怖いので、タプルにしておきます
# 1番目が1の場合以外は考えなくていいので、(1,)だけやります
ans = 0
score = dfs((1,))
ans = max(ans, score)
print(ans)
# Original solution
from _collections import deque

def cacl(seq):
    score = 0
    for a, b, c, d in req:
        if seq[b-1] - seq[a-1] == c:
            score += d
    return score

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

ans = 0
que = deque()

for i in range(1, m+1):
    que.append([i])

while que:
    seq = que.popleft()

    if len(seq) == n:
        score = cacl(seq)
        ans = max(ans, score)
    else:
        for i in range(seq[-1], m + 1):
            seq_next = seq + [i]
            que.append(seq_next)
print(ans)

# Optimized solution
def dfs(seq):
    ans = 0

    if len(seq) == n:
        score = 0
        for a, b, c, d in req:
            if seq[b-1] - seq[a-1] == c:
                score += d
        return score
    else:
        for i in range(seq[-1], m+1):
            next_seq = seq + (i,)
            score = dfs(next_seq)
            ans = max(ans, score)
    return ans

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

print((dfs((1,))))
# Original solution
n, m, q = list(map(int, input().split()))
l = [[int(i) for i in input().split()] for _ in range(q)]
m -= 1
As = []


def gen_A(x, y, A, c):
  global As
  if x == n and y == m:
    As.append(A)
    return
  if x < n:
    nA = A+str(c)
    gen_A(x+1, y, nA, c)
  if y < m:
    nc = c+1
    gen_A(x, y+1, A, nc)


gen_A(0, 0, "", 0)
mx = 0
for A in As:
  pts = 0
  for i in l:
    if int(A[i[1]-1])-int(A[i[0]-1]) == i[2]:
      pts += i[3]
  mx = max(mx, pts)
print(mx)

# Optimized solution
n, m, q = list(map(int, input().split()))
l = [[int(i) for i in input().split()] for _ in range(q)]
m -= 1
mx = 0


def gen_A(x, y, A, c):
  global mx
  if x == n and y == m:
    pts = 0
    for i in l:
      if int(A[i[1]-1])-int(A[i[0]-1]) == i[2]:
        pts += i[3]
    mx = max(mx, pts)
    return
  if x < n:
    nA = A+str(c)
    gen_A(x+1, y, nA, c)
  if y < m:
    nc = c+1
    gen_A(x, y+1, A, nc)


gen_A(0, 0, "", 0)
print(mx)
# Original solution
import sys
input = sys.stdin.buffer.readline

from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict
from itertools import combinations, permutations
from itertools import accumulate
from math import ceil, sqrt, pi

MOD = 10 ** 9 + 7
INF = 10 ** 18


N, M, Q = list(map(int, input().split())) 
ABCD = [list(map(int, input().split())) for _ in  range(Q)]
#print(ABCD)

lst = []

def dfs(i, n, tmp):
    if n == N:
        lst.append(tmp)
        return 0
    for j in range(i, M + 1):
        dfs(j, n + 1, tmp + [j])

dfs(1, 0, [])
#print(lst)

ans = 0
for l in lst:
    tmp = 0
    for abcd in ABCD:
        a, b, c, d = abcd
        if l[b - 1] - l[a - 1] == c:
            tmp += d
    ans = max(tmp, ans)
    
print(ans)

# Optimized solution
import sys
input = sys.stdin.buffer.readline

from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split())) 
ABCD = [list(map(int, input().split())) for _ in  range(Q)]

lst = list(combinations_with_replacement(list(range(1, M + 1)), N))

ans = 0
for l in lst:
    tmp = 0
    for a, b, c, d in ABCD:
        if l[b - 1] - l[a - 1] == c:
            tmp += d
    ans = max(tmp, ans)
    
print(ans)
# Original solution
import itertools

N, M, Q = list(map(int, input().split()))
ABCD = [list(map(int, input().split())) for _ in range(Q)]

# 1 ~ Mまでの数を用いたN項の重複数列を作成する
arrs = []
for arr in itertools.combinations_with_replacement(list(range(1, M + 1)), N):
    arrs.append(list(arr))

answer = - float("inf")
for arr in arrs:
    tmp = 0
    for a, b, c, d in ABCD:
        if arr[b - 1] - arr[a - 1] == c:
            tmp += d
    answer = max(answer, tmp)

print(answer)

# Optimized solution
import itertools

N, M, Q = list(map(int, input().split()))
ABCD = [list(map(int, input().split())) for _ in range(Q)]

# 1 ~ Mまでの数を用いたN項の重複組合せを作成する
arrs = []
for arr in itertools.combinations_with_replacement(list(range(1, M + 1)), N):
    arrs.append(list(arr))
#print(len(arrs))

# 各組合せに対し、全クエリを実行して最大値を求める　(計算量: 10H10 x 50)
answer = - float("inf")
for arr in arrs:
    tmp = 0
    for a, b, c, d in ABCD:
        if arr[b - 1] - arr[a - 1] == c:
            tmp += d
    answer = max(answer, tmp)

print(answer)
# Original solution

import math
import itertools
import sys
import collections

def solve(N,M,Q,ABCD):
    ans = 0
    A = [i for i in range(1,M+1)]
    for p in itertools.combinations_with_replacement(A,N):
        tmp = 0
        for a,b,c,d in ABCD:
            if(p[b-1] - p [a-1] == c):
                tmp+=d
        ans = max(ans,tmp)
    return ans



def main():
    N ,M,Q = list(map(int,input().split()))
    ABCD = []
    for _ in range(Q):
        ABCD.append(list(map(int,input().split())))
    # solve()
    print((solve(N,M,Q,ABCD)))
if __name__ == '__main__':
   main()

# Optimized solution

import math
import itertools

def solve(N:int,M:int,Q:int,ABCD:list) -> int:
    ans = 0
    elements = [i for i in range(1,M+1)] # 数列Aにあり得る要素
    # 条件を満たす数列Aを作成
    for A in itertools.combinations_with_replacement(elements,N):
        total = 0 # 現在の数列の得点を格納する変数
        for a,b,c,d in ABCD:
            if(A[b-1] - A [a-1] == c): # 条件を満たしていたら得点を追加
                total+=d
        ans = max(ans,total) # 最高得点を更新
    return ans



def main():
    N ,M,Q = list(map(int,input().split()))
    ABCD = []
    for _ in range(Q):
        ABCD.append(list(map(int,input().split())))
    # solve()
    print((solve(N,M,Q,ABCD)))
if __name__ == '__main__':
   main()
# Original solution
def main():
    import sys
    input = sys.stdin.buffer.readline
    N, M, Q = (int(i) for i in input().split())
    T = [[int(i) for i in input().split()] for j in range(Q)]

    def dfs(A):
        cur = 0
        if len(A) == N:
            for a, b, c, d in T:
                if A[b-1] - A[a-1] == c:
                    cur += d
        elif len(A) == 0:
            for i in range(1, M+1):
                v = dfs(A + [i])
                cur = max(cur, v)
        else:
            for i in range(A[-1], M+1):
                v = dfs(A + [i])
                cur = max(cur, v)
        return cur

    print((dfs([])))


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    from itertools import combinations_with_replacement
    import sys
    input = sys.stdin.buffer.readline
    N, M, Q = (int(i) for i in input().split())
    T = [[int(i) for i in input().split()] for j in range(Q)]
    ans = 0
    for C in combinations_with_replacement(list(range(1, M+1)), N):
        A = sorted(C)
        cur = 0
        for a, b, c, d in T:
            if A[b-1] - A[a-1] == c:
                cur += d
        ans = max(ans, cur)
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
import itertools

N, M, Q = list(map(int, input().split()))
a = []
b = []
c = []
d = []

for i in range(Q):
    ai, bi, ci, di = list(map(int, input().split()))
    a.append(ai)
    b.append(bi)
    c.append(ci)
    d.append(di)

ans = 0
for A in list(itertools.combinations_with_replacement(list(range(1, M+1)), N)):
#    print(A)                                                                                          
    score = 0
    for i in range(Q):
        if A[b[i]-1] - A[a[i]-1] == c[i]:
            score += d[i]
    if ans < score:
        ans = score

#    print(A, score)                                                                                   
print(ans)


# Optimized solution
import itertools

def main():
    N, M, Q = list(map(int, input().split()))
    a = []
    b = []
    c = []
    d = []

    for i in range(Q):
        ai, bi, ci, di = list(map(int, input().split()))
        a.append(ai)
        b.append(bi)
        c.append(ci)
        d.append(di)

    ans = 0
    for A in list(itertools.combinations_with_replacement(list(range(1, M+1)), N)):
#    print(A)                                                                                          
        score = 0
        for i in range(Q):
            if A[b[i]-1] - A[a[i]-1] == c[i]:
                score += d[i]
        if ans < score:
            ans = score

    print(ans)

if __name__ == "__main__":
    main()
# Original solution
import sys
sys.setrecursionlimit(10**9)
N, M ,Q = list(map(int, input().split()))
abcd = [0 for _ in range(Q)]
for i in range(Q):
    abcd[i] = list(map(int, input().split()))

ans = 0
def dfs(A):
    global ans
    if len(A) == N:
        score = 0
        for i in range(Q):
            if A[abcd[i][1]-1] - A[abcd[i][0]-1] == abcd[i][2]:
                score += abcd[i][3]
        ans = max(ans, score)
    else:
        for i in range(A[-1], M+1):
            dfs(A + [i])

for j in range(1, M+1):
    dfs([j])
    
print(ans)


# Optimized solution
import sys
sys.setrecursionlimit(10**9)
N, M ,Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
def dfs(A):
    global ans
    if len(A) == N:
        score = 0
        for a,b,c,d in abcd:
            if A[b-1] - A[a-1] == c:
                score += d
        ans = max(ans, score)
    else:
        for i in range(A[-1], M+1):
            dfs(A + [i])

for j in range(1, M+1):
    dfs([j])
    
print(ans)
# Original solution
import sys
sys.setrecursionlimit(10**7)

N, M, Q = list(map(int, input().split()))
arr = [list(map(int, input().split())) for _ in range(Q)]
ans = set()

def generate_arr(target):
    if len(target) == N:
        tmp = 0
        for item in arr:
            if target[item[1]-1] - target[item[0]-1] == item[2]:
                tmp += item[3]
            ans.add(tmp)
        return
    for i in range(target[-1],M+1):
        next_target = target.copy()
        next_target.append(i)
        generate_arr(next_target)
for i in range(1,M+1):
    generate_arr([i])
print((max(ans)))

# Optimized solution
target = []
def dfs(A):
    if len(A) == N:
        tmp = 0
        for a, b, c, d in arr:
            if A[b-1] - A[a-1] == c:
                tmp += d
        target.append(tmp)
        return
    start = 1
    if len(A) != 0:
        start = A[-1]
    for v in range(start,M+1):
        A.append(v)
        dfs(A)
        A.pop()
N, M, Q = list(map(int, input().split()))
arr = [list(map(int, input().split())) for _ in range(Q)]
dfs([])
print((max(target)))
# Original solution
import itertools

N, M, Q = list(map(int, input().split()))
A = list(itertools.combinations_with_replacement(list(range(1, M + 1)), N))
B = []
# print(len(list(A)))
for a in A:
    for j in range(N - 1):
        flag = True
        if a[j] > a[j + 1]:
            flag = False
            break
    if flag:
        B.append(a)

a = []
b = []
c = []
d = []

for i in range(Q):
    o, p, q, r = list(map(int, input().split()))
    o -= 1
    p -= 1
    a.append(o)
    b.append(p)
    c.append(q)
    d.append(r)

ans = 0
for li in B:
    tmp = 0
    for i in range(Q):
        if li[b[i]] - li[a[i]] == c[i]:
            tmp += d[i]
    ans = max(ans, tmp)
print(ans)


# Optimized solution
N, M, Q = list(map(int, input().split()))

a = []
b = []
c = []
d = []

ans = 0


def dfs(A):
    global ans
    if len(A) == N:
        now = 0
        for i in range(Q):
            if A[b[i]] - A[a[i]] == c[i]:
                now += d[i]
        ans = max(now, ans)
        return

    if not A:  # Aが空リストのとき
        tmp = 1
    else:
        tmp = A[-1]

    while tmp <= M:
        dfs(A + [tmp])
        tmp += 1


for i in range(Q):
    o, p, q, r = list(map(int, input().split()))
    o -= 1
    p -= 1
    a.append(o)
    b.append(p)
    c.append(q)
    d.append(r)
dfs([])
print(ans)
# Original solution
# -*- coding: utf-8 -*-

# sys.setrecursionlimit(100000)
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)

def slv(N, M, Q, ABCD):

    def dfs(a, n):
        r = 0
        if n == N:
            t = 0
            for abcd in ABCD:
                A, B, C, D = abcd
                if a[B] - a[A] == C:
                    t += D
            return t
        b = a[-1]
        for c in range(b, M+1):
            a.append(c)
            r = max(r, dfs(a, n+1))
            a.pop()
        return r
    return dfs([1], 0)


def main():
    N, M, Q = read_int_n()
    ABCD = [read_int_n() for _ in range(Q)]
    print(slv(N, M, Q, ABCD))


if __name__ == '__main__':
    main()


# Optimized solution
# -*- coding: utf-8 -*-
from itertools import combinations_with_replacement


def read_int_n():
    return list(map(int, input().split()))


def slv(N, M, Q, ABCD):
    ans = 0
    for a in combinations_with_replacement(list(range(1, M+1)), r=N):
        t = 0
        for abcd in ABCD:
            A, B, C, D = abcd
            if a[B-1] - a[A-1] == C:
                t += D
        ans = max(ans, t)
    return ans


def main():
    N, M, Q = read_int_n()
    ABCD = [read_int_n() for _ in range(Q)]
    print((slv(N, M, Q, ABCD)))


if __name__ == '__main__':
    main()
# Original solution
nmq=list(map(int, input().split()))
N=nmq[0]
M=nmq[1]
Q=nmq[2]
ablist=[]
for i in range(Q):
    tp=tuple(map(int, input().split()))
    ablist.append(tp)
sumlist=[]
alist=[i for i in range(1,M+1)]
import itertools as ite
case=list(ite.combinations_with_replacement(alist,N))
for nowcase in case:
    sumd=0
    for i in range(Q):
        a=ablist[i][0]
        b=ablist[i][1]
        c=ablist[i][2]
        d=ablist[i][3]
        aa=nowcase[a-1]
        ab=nowcase[b-1]
        if ab-aa==c:
            sumd+=d
    sumlist.append(sumd)
print((max(sumlist)))


# Optimized solution
nmq=list(map(int, input().split()))
N=nmq[0]
M=nmq[1]
Q=nmq[2]
ablist=[]
for i in range(Q):
    tp=tuple(map(int, input().split()))
    ablist.append(tp)
sumlist=[]
alist=[i for i in range(1,M+1)]
import itertools
case=list(itertools.combinations_with_replacement(alist,N))
for nowcase in case:
    sumd=0
    for i in range(Q):
        if nowcase[ablist[i][1]-1]-nowcase[ablist[i][0]-1]==ablist[i][2]:
            sumd+=ablist[i][3]
    sumlist.append(sumd)
print((max(sumlist)))
# Original solution
N, M, Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for _ in range(Q)]

m = M - 1

def f(s):
    if len(s) == N:
        return [s]
    result = []
    if s == '':
        r = 0
    else:
        r = int(s[-1])
    for i in range(r, m + 1):
        result.extend(f(s + str(i)))
    return result

result = 0
V = f('')
for s in V:
    A = [int(c) + 1 for c in s]
    t = 0
    for a, b, c, d in abcd:
        if A[b - 1] - A[a - 1] == c:
            t += d
    result = max(result, t)
print(result)


# Optimized solution
from itertools import combinations_with_replacement

N, M, Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for _ in range(Q)]

result = 0
for A in combinations_with_replacement(list(range(1, M + 1)), N):
    t = 0
    for a, b, c, d in abcd:
        if A[b - 1] - A[a - 1] == c:
            t += d
    result = max(result, t)
print(result)
# Original solution
import itertools

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

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

M=0
for l in map(list,itertools.combinations(list(range(1,n+m)),n)):
    s=0
    for j in range(q):
        aj=lis[j][0]
        bj=lis[j][1]
        cj=lis[j][2]
        dj=lis[j][3]
        if (l[bj-1]-(bj-1))-(l[aj-1]-(aj-1))==cj:
            s+=dj
    M=max(M,s)
print(M)

# Optimized solution
import itertools

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

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

M=0
for l in map(list,itertools.combinations(list(range(1,n+m)),n)):
    s=0
    for j in range(q):
        aj=lis[j][0]
        bj=lis[j][1]
        cj=lis[j][2]
        dj=lis[j][3]
        if l[bj-1]-bj-l[aj-1]+aj==cj:
            s+=dj
    M=max(M,s)
print(M)
# Original solution
import itertools
N,M,Q = list(map(int,input().split()))

num_all = [i+1 for i in range(M)]
#print(num_all)
Comb = list(itertools.combinations_with_replacement(num_all,N))

num_l = []         
for i in range(Q):
  a,b,c,d = list(map(int,input().split()))
  num_l.append([a,b,c,d])
#print(num_l)  
  
#print(len(Comb))
d_sum_max = 0  
for A in Comb:
  d_sum = 0
  #print(A)
  #print()
  
  for i in range(Q):
    a,b,c,d = num_l[i][0],num_l[i][1],num_l[i][2],num_l[i][3]
    #print(a,b,c,d)
    #print(A[b-1],A[a-1],':',A[b-1]-A[a-1],c)
    if(A[b-1]-A[a-1] == c):
      d_sum += d
      
  #print(A,d_sum)
  d_sum_max = max(d_sum_max,d_sum)

print(d_sum_max)    

# Optimized solution
import itertools
N,M,Q = list(map(int,input().split()))

num_all = [i+1 for i in range(M)]
#print(num_all)
Comb = list(itertools.combinations_with_replacement(num_all,N))

num_l = []         
for i in range(Q):
  a,b,c,d = list(map(int,input().split()))
  num_l.append([a,b,c,d])
#print(num_l)  
  
#print(len(Comb))
d_sum_max = 0  
for A in Comb:
  d_sum = 0
  #print(A)
  #print()
  
  for x in num_l:
    a,b,c,d = x[0],x[1],x[2],x[3]
    #print(a,b,c,d)
    #print(A[b-1],A[a-1],':',A[b-1]-A[a-1],c)
    if(A[b-1]-A[a-1] == c):
      d_sum += d
      
  #print(A,d_sum)
  d_sum_max = max(d_sum_max,d_sum)

print(d_sum_max)