Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i , c_i , d_i ).
Consider a sequence A satisfying the following conditions:
 - A is a sequence of N positive integers.
 - 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
 - The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A.

-----Constraints-----
 - All values in input are integers.
 - 2 ≤ N ≤ 10
 - 1 \leq M \leq 10
 - 1 \leq Q \leq 50
 - 1 \leq a_i < b_i \leq N ( i = 1, 2, ..., Q )
 - 0 \leq c_i \leq M - 1 ( i = 1, 2, ..., Q )
 - (a_i, b_i, c_i) \neq (a_j, b_j, c_j) (where i \neq j)
 - 1 \leq d_i \leq 10^5 ( i = 1, 2, ..., Q )

-----Input-----
Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q

-----Output-----
Print the maximum possible score of A.

-----Sample Input-----
3 4 3
1 3 3 100
1 2 2 10
2 3 2 10

-----Sample Output-----
110

When A = \{1, 3, 4\}, its score is 110. Under these conditions, no sequence has a score greater than 110, so the answer is 110.

Difficulty: introductory

Starter code:

Solutions:
N, M, Q = map(int, input().split())
A = []
for i in range(Q):
  A.append(list(map(int, input().split())))
def f(B, n):
  ans = 0
  if len(B) < N:
    for i in range(n, M+1):
      ans = max(ans, f(B+[i], i))
  else:
    for j in range(Q):
      if B[A[j][1]-1] - B[A[j][0]-1] == A[j][2]:
        ans += A[j][3]
    return ans
  return ans
print(f([], 1))

import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n,m,q = LI()
    require = [LI() for _ in range(q)]
    itr = itertools.combinations_with_replacement(list(range(1,n+2)),m-1)     
    ans = 0
    for i in itr:
        lst = [0 for _ in range(n+2)]
        lst[0] = 1
        cnt = 0
        for j in i:
            lst[j] += 1
        lst = list(itertools.accumulate(lst))
        for re in require:
            if lst[re[1]]-lst[re[0]]==re[2]:
                cnt += re[3]
        ans = max(cnt,ans)
    print(ans)

main()

from itertools import combinations_with_replacement as comb_rplc
N,M,Q = list(map(int,input().split()))
array = [ list(map(int,input().split())) for _ in range(Q) ]
ans = 0
for seq in comb_rplc(list(range(1, M + 1)), N):
  score = 0
  for a,b,c,d in array:
    if seq[b-1] - seq [a-1] == c:
      score += d
  ans = max(score,ans)
print(ans)
    


import sys
import itertools

N,M,Q = map(int, input().split())
A = []
B = []
C = []
D = []
for i in range(Q):
    a,b,c,d = map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
    D.append(d)

num = list(range(1,M+1))
ans = 0
for tpl in itertools.combinations_with_replacement(num,N):
    point = 0
    for i in range(Q):
        if tpl[B[i]-1] - tpl[A[i]-1] == C[i]:
            point += D[i]
    
    ans = max(ans,point)

print(ans)

import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import combinations_with_replacement
import numpy as np
def main():
    n, m, q = map(int, input().split())
    A = np.array(list(combinations_with_replacement(range(1, m + 1), n)))
    numA = len(A)
    score = np.zeros(numA, np.int32)
    ma = map(int, read().split())
    for a, b, c, d in zip(ma, ma, ma, ma):
        a -= 1
        b -= 1
        eachA_is_equalOrNot = A[:, b] - A[:, a] == c
        score += d * eachA_is_equalOrNot
        # print(score)
    print(score.max())

def __starting_point():
    main()
__starting_point()

import itertools
n,m,q=map(int,input().split())
abcd=[list(map(int,input().split())) for _ in range(q)]
ans=0
for A in itertools.combinations_with_replacement(range(1,m+1),n):
  score=0
  for a,b,c,d in abcd:
    if A[b-1]-A[a-1]==c:
      score+=d
  ans=max(ans,score)
print(ans)

from itertools import combinations_with_replacement as c
N, M, Q = map(int,input().split())
l1 = list(list(map(int,input().split())) for _ in range(Q))

ans = 0

for l2 in c(range(1, M + 1), N):
  total = 0
  for i in range(Q):
    if l2[l1[i][1] - 1] - l2[l1[i][0] - 1] == l1[i][2]:
      total += l1[i][3]
  ans = max(ans, total)

print(ans)

from itertools import combinations_with_replacement

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

cands = []
for i in combinations_with_replacement(list(range(1,m+1)),n):
    cands.append(i)

maxi = 0
for cand in cands:
    score = 0
    for condition in conditions:
        if cand[condition[1]-1] - cand[condition[0]-1]  == condition[2]:
            score += condition[3]
    if score > maxi:
        maxi = score
print(maxi)

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

def calc(A):
  score=0
  for a,b,c,d in abcd:
    if A[b-1]-A[a-1]==c:
      score+=d
  return score

def dfs(A,i):
  nonlocal ans
  if len(A)==n:
    ans=max(ans,calc(A))
    return
  for j in range(i,m+1):
    A.append(j)
    dfs(A,j)
    A.pop()

dfs([],1)
print(ans)

def dfs(s):
    nonlocal ans
    if len(s) == n:
        cnt = 0
        for i in range(q):
            if s[abcd[i][1]-1]-s[abcd[i][0]-1] == abcd[i][2]:
                cnt += abcd[i][3]
        ans = max(ans, cnt)
        return
    last = 1
    if len(s) > 0:
        last = s[-1]
    for i in range(last, m+1):
        dfs(s+[i])
    return

n, m, q = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(q)]
ans = 0
dfs([])
print(ans)

import itertools
N, M, Q = map(int,input().split())
ABCD_list = [[int(_) for _ in input().split()] for i in range(Q)]
combs = itertools.combinations_with_replacement(range(1,M+1),N)
points = []
for comb in combs:
    A_list = list(comb)
    point = 0
    for inner in ABCD_list:
        if A_list[inner[1]-1]-A_list[inner[0]-1] == inner[2]:
            point += inner[3]
        points.append(point)
print(max(points))

import itertools
n,m,q = map(int, input().split())
a,b,c,d = [0]*q,[0]*q,[0]*q,[0]*q

for i in range(q):
    a[i],b[i],c[i],d[i] = map(int, input().split())

ans = 0

for C in itertools.combinations_with_replacement(range(1,m+1),n):
    x = 0
    for i in range(q):
        if C[b[i]-1] - C[a[i]-1] == c[i]:
            x += d[i]

    ans = max(ans,x)

print(ans)

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):
        len_lst = len(lst)
        if len_lst == n:
            return calc_point(lst)
        point = 0
        last_elm = lst[-1] if len_lst > 0 else 1
        for i in range(last_elm, m+1):
            point = max(point, dfs(lst+[i]))
        return point

    print(dfs([]))


def __starting_point():
    main()
__starting_point()

from itertools import combinations_with_replacement as combi

N, M, Q = map(int, input().split())

a = [0] * 55
b = [0] * 55
c = [0] * 55
d = [0] * 55

for i in range(Q):
    a[i], b[i], c[i], d[i] = map(int, input().split())

candidate = combi(range(1, M+1), N)

ans = 0

for A in candidate:
    score = 0
    for i in range(Q):
        if A[b[i]-1] - A[a[i]-1] == c[i]:
            score += d[i]

    ans = max(ans, score)

print(ans)

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] if lst else 1
        for i in range(last_elm, m+1):
            point = max(point, dfs(lst+[i]))
        return point

    print(dfs([]))


def __starting_point():
    main()
__starting_point()

# 参考 : https://atcoder.jp/contests/abc165/submissions/16921279
# 参考 : https://atcoder.jp/contests/abc165/submissions/16832189
from collections import deque
n,m,q = list(map(int,input().split()))
l = [list(map(int,input().split())) for i in range(q)]
a,b,c,d = [list(i) for i in zip(*l)]
# 数列 A の基(最初は1を入れておく)
queue = deque([[1]])
ans = 0
# 数列 A の候補がなくなる(キューが空になる)まで探索
while queue:
    # 数列 A を前から一つ取り出す
    x = queue.popleft()
    # 数列 A が長さ N か
    if len(x) == n:
        s = 0
        # 実際に何点取得できるか
        for i in range(q):
            if x[b[i]-1] - x[a[i]-1] == c[i]:
                s += d[i]
        ans = max(ans,s)
    else:
        # 違う場合、後ろに数字を付け足す
        # 付け足す数字は取り出した数列 A の一番後ろの値から M まで
        # 最終的に長さが 1 , 2 ... N となり、if の条件を満たすようになる
        for j in range(x[-1],m+1):
            y = x + [j]
            queue.append(y)
print(ans)
# 制約
# 2 <= N <= 10
# 1 <= M <= 50
# 1 <= Q <= 50
# なのでこの探索は間に合う
# 入力例 1 は A[1,3,4]
# 入力例 2 は A[1,1,1,4]
# 入力例 3 は A[1,1,1,1,1,1,1,1,1,10]
# が最大得点となる


from itertools import combinations_with_replacement

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

point_max = 0
for A in combinations_with_replacement(range(1, M+1), N):
    point = 0
    for a, b, c, d in ABCD:
        if A[b-1]-A[a-1] == c:
            point += d
    point_max = max(point, point_max)

print(point_max)

import itertools

N, M, Q = map(int, input().split())

seisu_list = []

for i in range(Q):
    seisu = list(map(int, input().split()))
    seisu_list.append(seisu)

M_list = [i for i in range(1,M+1)]
A_list = list(itertools.combinations_with_replacement(M_list,N))
#print(A_list)
ans = 0

for i in range(len(A_list)):
    ans_temp = 0
    for j in range(Q):
        if A_list[i][seisu_list[j][1]-1] - A_list[i][seisu_list[j][0]-1] == seisu_list[j][2]:
            ans_temp += seisu_list[j][3]
            ans = max(ans, ans_temp)
print(ans)

N, M, Q = map(int, input().split())
ABCD = [[]for i in range(Q)]
for i in range(Q):
    ABCD[i] = list(map(int, input().split()))
ans = 0
import itertools
for p in itertools.combinations_with_replacement([x for x in range(1, M + 1)], N):
    point = 0
    p = list(p)
    for a, b, c, d in ABCD:
        if p[b-1] - p[a-1] == c: point += d
    ans = max(ans, point)
print(ans)

from itertools import combinations_with_replacement
N, M, Q = map(int,input().split())
ls = []
for _ in range(Q):
    a,b,c,d = map(int,input().split())
    ls.append((a-1,b-1,c,d))
ans = -1
for A in combinations_with_replacement(range(1,M+1),r=N):
    sm = 0
    for a,b,c,d in ls:
        if A[b]-A[a] == c:
            sm += d
    ans = max(ans, sm)
print(ans)

# -*- coding: utf-8 -*-
"""
Created on Thu Sep  3 23:43:13 2020

@author: liang
"""

N ,M ,Q = map(int, input().split())

A = list()
lis = list()
ans = 0

def make_list(n,m):
    if n == N:
        A.append(lis.copy())
        return 
    for i in range(m,M+1):
        lis.append(i)
        make_list(n+1,i)
        lis.pop()

make_list(0,1)
#print(A)

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

for a in A:
    tmp = 0
    for c in calc:
        if a[c[1]-1] - a[c[0]-1] == c[2]:
            tmp += c[3]
    if tmp > ans:
        ans = tmp
       
print(ans)

import copy

def main():
	N, M, Q = [int(n) for n in input().split(" ")]
	q = [[int(a) for a in input().split(" ")] for i in range(Q)]
	all_series = get_series(N, M)
	points = [0]
	for l in all_series:
		points.append(get_score(l, q))
	print(max(points))

def get_score(l, q):
	return sum([q[i][3] if l[q[i][1] - 1] - l[q[i][0] - 1] == q[i][2] else 0 for i in range(len(q))])

def get_series(N, M):
	# N: number of elms
	# M: upper limit of val of elm
	all_series = []
	checked = [[0] * M for i in range(N)]
	to_check = [[0, j + 1] for j in range(M)]
	series = [0 for k in range(N)]
	while len(to_check) > 0:
		checking = to_check.pop(-1)
		series[checking[0]] = checking[1]
		if checking[0] == N - 1:
			l = copy.deepcopy(series)
			all_series.append(l)
		else:
			to_check.extend([[checking[0] + 1, k] for k in range(checking[1], M + 1)])
	return all_series

main()

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

def check(x):
    #print(x)
    z = [0] * n
    temp = 0
    y = 0
    for p in x:
        if p == 0:
            temp += 1
        else:
            z[y] = temp
            y += 1
    #print(z)
    ans = 0
    for a, b, c, d in abcd:
        if z[b-1] - z[a-1] == c:
            ans += d
    return ans

def func(x, s, t):
    if s == m:
        temp = [] + x
        while t < n:
            temp.append(1)
            t += 1
        return check(temp)
    ans = func(x + [0], s+1, t)
    if t == n:
        return ans
    else:
        return max(ans, func(x + [1], s, t+1))

print(func([0], 1, 0))

#!/usr/bin/env python3
# スペース区切りの整数の入力

def dfs(seq):
    ans = 0
    if len(seq) == N:
        score_ret = 0
        for a, b, c, d in data:
            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,)
            score = dfs(seq_next)
            ans = max(ans, score)
    
    return ans

# スペース区切りの整数の入力
N, M, Q = list(map(int, input().split()))
#配列の入力
data = [list(map(int, input().split())) for _ in range(Q)]

ans = -1
score = dfs((1,))
ans = max(ans, score)
print(ans)


def backtrack(nums, N, cur_state, result):
    if len(cur_state) == N:
        result.append(cur_state[:])
    else:
        for i in range(len(nums)):
            cur_state.append(nums[i])
            backtrack(nums[i:], N, cur_state, result)
            cur_state.pop()

def solve():
    N,M,Q = [int(i) for i in input().split()]
    quads = []
    for i in range(Q):
        quads.append([int(i) for i in input().split()])
    candidates = []
    backtrack(list(range(1, M+1)), N, [], candidates)

    ans = 0
    for candidate in candidates:
        score = 0
        for a,b,c,d in quads:
            if candidate[b-1] - candidate[a-1] == c:
                score += d
        ans = max(score, ans)
    print(ans)

def __starting_point():
    solve()
__starting_point()

#12653415
#わからんが、とてもきれいですね

N, M, Q = list(map(int, input().split()))
 
conditions = [list(map(int, input().split())) for _ in range(Q)]
 
def dfs(A, max_a):
    if len(A) == N:
        num = 0
        for a, b, c, d in conditions:
            if A[b-1] - A[a-1] == c:
                num += d
        return num
    ans = 0
    for i in range(max_a, M+1):
        ans = max(ans, dfs(A + [i], i))
    return ans
 
print(dfs([1], 1))

import itertools
n,m,q = map(int,input().split())
l = []
for i in range(q):
  tl = list(map(int,input().split()))
  l.append(tl)
#print(l)

lis = [i+1 for i in range(m)]
lis = list(itertools.combinations_with_replacement(lis, n))
#print(lis)
ans = 0
for i in lis:
  tmp = 0
  for j in range(q):
    a = l[j][1] - 1
    b = l[j][0] - 1
    if l[j][2] == i[a] - i[b]:
      tmp += l[j][3]
  ans = max(tmp,ans)
print(ans)

N,M,Q=list(map(int,input().split()))
from itertools import combinations_with_replacement as comb
l=comb(range(1,M+1),N)
q=[list(map(int,input().split())) for i in range(Q)]
ans=0
for i in l:
   tmp=0
   for a,b,c,d in q:
      if i[b-1]-i[a-1]==c:
         tmp+=d
   ans=max(tmp,ans)
print(ans)

#C - Many Requirements
#再帰関数(n重のfor文)
N,M,Q = map(int,input().split())
abcd = []
for _ in range(Q):
    a,b,c,d = map(int,input().split())
    abcd.append((a,b,c,d))
max_score = 0
def dfs(A):
    nonlocal max_score
    if len(A) == N:
        #処理
        score = 0
        for ai,bi,ci,di in abcd:
            if A[bi-1] -A[ai-1] == ci:
                score += di
        return score
    #一つ前の値を保持
    if len(A) >= 1:
        prev_last = A[-1]
    else:
        prev_last = 1
    #1つ前の値より大きい値の範囲
    for i in range(prev_last,M+1):
        A.append(i)
        max_score = max(max_score,dfs(A))
        A.pop()
    return max_score#ループの途中のdfs (len(A) != N のとき) に値を返す
dfs([])
print(max_score)

import sys
import math
import itertools
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
    
def main():
    N, M, Q = MI()
    L = [LI() for i in range(Q)]
    A = [i for i in range(1, M+1)]
    ans = 0

    for i in itertools.combinations_with_replacement(A, N):
        temp = 0
        for a,b,c,d in L:
            if i[b-1] - i[a-1] == c:
                temp+=d
        if temp > ans:
            ans = temp
    
    print(ans)

    
def __starting_point():
    main()
__starting_point()

import itertools
N,M,Q=list(map(int,input().split()))
a=[0]*Q;b=[0]*Q;c=[0]*Q;d=[0]*Q
for i in range(Q):
  a[i],b[i],c[i],d[i]=list(map(int,input().split()))
l=list(range(1,M+1))

i=0;D=[]
for v in itertools.combinations_with_replacement(l, N):
  D.append(0)
  for j in range(Q):
    if v[b[j]-1]-v[a[j]-1]==c[j]:
      D[i]+=d[j]
  i+=1
print((max(D)))


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

ans = 0
for ary in combinations_with_replacement(range(1, m+1), n):
    p = 0
    for a,b,c,d in abcd:
        if ary[b-1] - ary[a-1] == c:
            p += d
    ans = max(ans, p)

print(ans)

import itertools
n,m,q = map(int,input().split())
abcd = [list(map(int,input().split())) for _ in range(q)]
maxpt = 0
for lis in itertools.combinations_with_replacement(range(1, m+1), n):
    forpt = 0
    for i in range(q):
        if lis[abcd[i][1]-1] - lis[abcd[i][0]-1] == abcd[i][2]:
            forpt += abcd[i][3]
    maxpt = max(maxpt,forpt)
print(maxpt)

import itertools
from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, list(range(n, n - r, -1)), 1)
    denom = reduce(mul, list(range(1, r + 1)), 1)
    return numer // denom
  
N,M,Q=list(map(int,input().split()))
a=[0]*Q;b=[0]*Q;c=[0]*Q;d=[0]*Q
for i in range(Q):
  a[i],b[i],c[i],d[i]=list(map(int,input().split()))
l=list(range(1,M+1))


h=combinations_count(M+N-1, N)
H=[0]*h;i=0
for v in itertools.combinations_with_replacement(l, N):
    H[i]=v
    i+=1

D=[0 for j in range(h)]

for i in range(h):
  for j in range(Q):
    if H[i][b[j]-1]-H[i][a[j]-1]==c[j]:
      D[i]+=d[j]
  
print((max(D)))


import sys
sys.setrecursionlimit(10 ** 6)
N, M, Q = map(int, input().split())
Q = [list(map(int,input().split())) for _ in range(Q)]
ans = 0
A = []


def dfs():
    nonlocal A
    if len(A) == N:
        return
    nonlocal ans
    if len(A) == 0:
        start = 1
    else:
        start = A[-1]
    for i in range(start, M + 1):
        A.append(i)
        dfs()
        if len(A) != N:
            A.pop()
            continue
        # print(A)
        tmp_ans = 0
        for a, b, c, d in Q:
            if A[b - 1] - A[a - 1] == c:
                tmp_ans += d
        ans = max(ans, tmp_ans)
        A.pop()


dfs()
print(ans)

from itertools import combinations_with_replacement as cwr
N,M,Q=map(int,input().split())
ABCD=[]
for _ in range(Q):
    ABCD.append(list(map(int,input().split())))
maxscore=0
for comb in cwr(range(1,M+1),N):
    score=0
    for row in ABCD:
        if comb[row[1]-1]-comb[row[0]-1]==row[2]:
            score+=row[3]
    maxscore=max(maxscore,score)
print(maxscore)

import itertools

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

ans=0
for A in itertools.combinations_with_replacement([m for m in range(1,M+1)], N):
  ans2=0
  for (a,b,c,d) in ABCD:
    if A[b-1]-A[a-1]==c:
      ans2+=d
  ans=max(ans, ans2)
print(ans)


import itertools

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

ans=0
t=[m for m in range(1,M+1)]
for A in itertools.combinations_with_replacement(t, N):
  ans2=0
  for (a,b,c,d) in ABCD:
    if A[b-1]-A[a-1]==c:
      ans2+=d
  ans=max(ans, ans2)
print(ans)


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

candidate = []


def gen(cur):
    if len(cur) == n:
        candidate.append(cur)
    else:
        t = cur[-1]
        for tv in range(t, m + 1):
            nex = cur[:]
            nex.append(tv)
            gen(nex)


for i in range(1, 10):
    arr = [i]
    gen(arr)

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

ans = 0
for cv in candidate:
    tmp = 0
    for av in ask:
        if cv[av[1] - 1] - cv[av[0] - 1] == av[2]:
            tmp += av[3]
    ans = max(ans, tmp)
print(ans)

import itertools
N, M, Q = map(int, input().split())
a = [0] * Q
b = [0] * Q
c = [0] * Q
d = [0] * Q
for i in range(Q):
    a[i], b[i], c[i], d[i] = map(int, input().split())

cc = [i for i in range(1, M+1)]
ans = 0

for A in itertools.combinations_with_replacement(cc, N):
    A = list(A)
    t = 0
    for i in range(Q):
        if A[b[i]-1] - A[a[i]-1] == c[i]:
            t += d[i]
    ans = max(ans, t)

print(ans)

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)

def __starting_point():
    main()

__starting_point()

n,m,q=map(int, input().split())
abcd=[]
for i in range(q):
  a,b,c,d=map(int, input().split())
  abcd.append([a,b,c,d])
def dfs(i,j):
  if i==0:
    alist=[]
    for k in range(j):
      alist.append([k+1])
    return alist
  elif i>0 and i<=n:
    blist=[]
    for k in dfs(i-1,j):
      last=k[-1]
      for l in range(last,j+1):
        clist=k.copy()
        clist.append(l)
        blist.append(clist)
    return blist
ans=0
for alist in dfs(n-1,m):
  cnt=0
  for i in range(q):
    if alist[abcd[i][1]-1]-alist[abcd[i][0]-1]==abcd[i][2]:
      cnt+=abcd[i][3]
  ans=max(ans,cnt)
print(ans)

from itertools import combinations_with_replacement

N, M, Q = map(int, input().split())
T = []
for q in range(Q):
    T.append(list(map(int, input().split())))

A = list(combinations_with_replacement(list(range(1, M+1)), N))
Alist = [list(a) for a in A]

#print(Alist)

Max = 0

for a in Alist:
    cost = 0
    for t in T:
        if a[t[1]-1] - a[t[0]-1] == t[2]:
            cost += t[3]
    if cost > Max:
        Max = cost
print(Max)

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] if len(lst) > 0 else 1
        for i in range(last_elm, m+1):
            point = max(point, dfs(lst+[i]))
        return point

    print(dfs([]))


def __starting_point():
    main()
__starting_point()

from collections import deque
def calc(seq):
  score = 0
  for a,b,c,d in array:
    if seq[b-1] - seq[a-1] == c:
      score += d
  return score
  
N,M,Q = map(int,input().split())
array = [ 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 = 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)

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

v = list(itertools.combinations_with_replacement(range(1, m+1), n))
ans = 0
for i in v:
    s = 0
    for a, b, c, d in abcd:
        if i[b-1] - i[a-1] == c:
            s += d
    ans = max(ans, s)
print(ans)

n,m,q=list(map(int,input().split()))
def dfs(seq):
    u=0
    if len(seq)==n:
        ans=0
        for a,b,c,d in data:
            if seq[b-1]-seq[a-1]==c:
                ans+=d
        return ans
    else:
        for i in range(seq[-1],m+1):
            seq_next=seq.copy()
            seq_next.append(i)
            u=max(u,dfs(seq_next))
        return u

data=[]
for _ in range(q):
    A=list(map(int,input().split()))
    data.append(A)
print((dfs([1])))


from itertools import combinations_with_replacement
N, M, Q = map(int, input().split())
ABCD = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for t in combinations_with_replacement(range(1, M+1), N):
    an = 0
    for a, b, c, d in ABCD:
        if t[b-1]-t[a-1] == c:
            an += d
    if ans < an:
        ans = an
print(ans)

import itertools
import sys

def score(abcd, A):
  ans = 0
  for a,b,c,d in abcd:
    if(A[b-1]-A[a-1])==c:
      ans += d
  return ans

N, M, Q = [int(x) for x in input().split()]
abcd = [ [int(x) for x in input().split()] for i in range(Q)]

a = itertools.combinations_with_replacement(range(1,M+1), N)
a = list(a)
Ans = 0
for i in range(len(a)):
  Ans = max(Ans,score(abcd,list(a[i])))
            
print(Ans)

from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def MAP1()  : return map(lambda x:int(x)-1,input().split())
def LIST()  : return list(MAP())

def solve():
    N, M, Q = MAP()
    A = [LIST() for _ in range(Q)]

    ans = 0
    for x in combinations_with_replacement(range(1, M+1), N):
        score = 0
        for a, b, c, d in A:
            if x[b-1] - x[a-1] == c:
                score += d
        ans = max(ans, score)
    print(ans)

def __starting_point():
    solve()
__starting_point()

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

nums = combinations_with_replacement(range(1,m+1), n)
ans = 0
for a in nums:
  t = 0
  for i in abcd:
    if a[i[1]-1] - a[i[0]-1] == i[2]: t += i[3]
  if ans < t: ans = t
print(ans)

import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import combinations_with_replacement
def main():
    n, m, q = list(map(int, input().split()))
    qs = []
    for _ in range(q):
        abcd = tuple(map(int, input().split()))
        qs.append(abcd)
    A = tuple(combinations_with_replacement(list(range(1, m+1)), n))
    r = 0
    for Ae in A:
        t0 = 0
        for qe in qs:
            if Ae[qe[1]-1] - Ae[qe[0]-1] == qe[2]:
                t0 += qe[3]
        r = max(r, t0)
    print(r)

def __starting_point():
    main()

__starting_point()

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]))


def __starting_point():
    main()
__starting_point()

import sys
sys.setrecursionlimit(10 ** 6)

N, M, Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for i in range(Q)]
A = []
def rec(itr, lst):
    if itr == N:
        res = 0
        for a, b, c, d in abcd:
            if A[b-1] - A[a-1] == c:
                res += d
        return res
    else:
        res = 0
        for i in range(lst, M):
            A.append(i)
            res = max(res, rec(itr + 1, i))
            A.pop()
        return res
print((rec(0, 0)))


N, M, Q = list(map(int, input().split()))
A = []

for i in range(Q):
  A.append(list(map(int, input().split())))

def calc(B, n):
  ans = 0
  #not enough length
  if len(B) < N:
    for i in range(n, M + 1):
      ans = max(ans, calc(B + [i], i))
  #enough length
  else:
    #print("B:", B)
    for j in range(Q):
      if B[A[j][1]-1] - B[A[j][0]-1] == A[j][2]:
        ans += A[j][3]
    #print("ans:", ans)
  return ans



print((calc([], 1)))


import numpy as np
import itertools
N, M, Q = list(map(int, input().split()))
G = []
for i in range(Q):
    a, b, c, d = list(map(int, input().split()))
    G.append([a, b, c, d])

ans = 0

A = np.array(list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)))

n = len(A)
score = np.zeros(n, np.int32)
for a, b, c, d in G:
    cond = A[:, b - 1] - A[:, a - 1] == c
    score += d * cond

print((score.max()))


import itertools
n,m,q=map(int,input().split())
abcd=[list(map(int,input().split())) for _ in range(q)]
ans=0
for A in itertools.combinations_with_replacement(range(1,m+1),n):
  score=0
  for a,b,c,d in abcd:
    if A[b-1]-A[a-1]==c:
      score+=d
  ans=max(ans,score)
print(ans)

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)


n,m,q=list(map(int,input().split()))
def dfs(seq):
    u=0
    if len(seq)==n:
        ans=0
        for a,b,c,d in data:
            if seq[b-1]-seq[a-1]==c:
                ans+=d
        return ans
    else:
        for i in range(seq[-1],m+1):
            seq_next=seq+[i]
            u=max(u,dfs(seq_next))
        return u

data=[]
for _ in range(q):
    A=list(map(int,input().split()))
    data.append(A)
print((dfs([1])))


import itertools
n,m,q=map(int,input().split())
d=[list(map(int,input().split())) for i in range(q)]
ans=0
for i in list(itertools.combinations_with_replacement(list(range(1,m+1)), n)):
  a=list(i)
  hantei=0
  for j in range(q):
    if (a[d[j][1]-1]-a[d[j][0]-1])==d[j][2]:
      hantei+=d[j][3]
  ans=max(ans,hantei)
print(ans)

# 入力
N, M, Q = map(int, input().split())
a = [0] * Q
b = [0] * Q
c = [0] * Q
d = [0] * Q
for i in range(Q):
    a[i], b[i], c[i], d[i] = map(int, input().split())
    a[i] -= 1
    b[i] -= 1

# スコア計算
def score(A):
    tmp = 0
    for ai, bi, ci, di in zip(a, b, c, d):
        if A[bi] - A[ai] == ci:
            tmp += di
    return tmp

# DFS
def dfs(A):
    if len(A) == N:
        return score(A) # 数列 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([]))

import itertools

def abc165c_many_requirements():
    n, m, q = map(int, input().split())
    all_list = []
    for _ in range(q):
        all_list.append(list(map(int, input().split())))

    pattern = itertools.combinations_with_replacement(range(1, m+1), n)
    best = 0
    for p in pattern:
        cost = 0
        for l in all_list:
            if p[l[1]-1] - p[l[0]-1] == l[2]:
                cost += l[3]
        if cost > best:
            best = cost
    print(best)

abc165c_many_requirements()

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

candidate = []


def gen(cur):
    if len(cur) == n:
        candidate.append(cur)
    else:
        t = cur[-1]
        for tv in range(t, m + 1):
            nex = cur[:]
            nex.append(tv)
            gen(nex)


for i in range(1, m + 1):
    arr = [i]
    gen(arr)

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

ans = 0
for cv in candidate:
    tmp = 0
    for av in ask:
        if cv[av[1] - 1] - cv[av[0] - 1] == av[2]:
            tmp += av[3]
    ans = max(ans, tmp)
print(ans)

# -*- coding: utf-8 -*-
from itertools import combinations_with_replacement
def main():
    n, m, q = list(map(int, input().split()))
    xs = []
    for i in range(q):
        xs.append(list(map(int, input().split())))
    max = 0
    for suretu in combinations_with_replacement(list(range(1, m + 1)), n):
        wa = 0
        for x in xs:
            a, b, c, d = x
            if suretu[b - 1] - suretu[a - 1] == c:
                wa += d
        if wa > max:
            max = wa
    print(max)
    

def __starting_point():
    main()

__starting_point()

N, M, Q = list(map(int, input().split()))
G = []
for i in range(Q):
    a, b, c, d = list(map(int, input().split()))
    G.append([a, b, c, d])

ans = 0


def dfs(s):
    nonlocal ans
    if len(s) == N:
        now = 0
        for a, b, c, d in G:
            if s[b - 1] - s[a - 1] == c:
                now += d
        ans = max(ans, now)
        return

    for i in range(s[-1], M):
        dfs(s + [i])


s = [0]
dfs(s.copy())

print(ans)


def dfs(lst):
    nonlocal ans
    if len(lst) > n:
        return
    lowest = 1
    if lst != []:
        lowest = lst[-1]
    for i in range(lowest, m+1):
        lst.append(i)
        dfs(lst)
        lst.pop()
    if len(lst) == n:
        cnt = 0
        for i in range(q):
            a, b, c, d = ab[i]
            if lst[b-1] - lst[a-1] == c:
                cnt += d
        ans = max(cnt, ans)

n,m,q = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0
dfs([])

print(ans)

N,M,Q = map(int, input().split())
a = [0]*Q
b = [0]*Q
c = [0]*Q
d = [0]*Q
for i in range(Q):
    a[i], b[i], c[i], d[i] = map(int, input().split())
    
def dfs(n_list, o, l, L):
    if o+l==0:
        num = 1
        pos = 0
        
        L_child = [0]*N
        for i in range(N+M-1):
            if n_list[i]==1:
                num += 1
            else:
                L_child[pos] = num
                pos += 1
        L.append(L_child)
    
    if o>=1:
        n_list[N+M-o-l-1] = 0
        dfs(n_list, o-1,l, L)
    if l>=1:
        n_list[N+M-o-l-1] = 1
        dfs(n_list, o, l-1, L)
    
A = [0]*(N+M-1)
L =[]
dfs(A,N,M-1,L)

ans = 0
for i in L:
    score = 0
    for j in range(Q):
        if i[b[j]-1]-i[a[j]-1]==c[j]:
            score += d[j]

    if score > ans:
        ans = score

print(ans)

def dfs(cnt, lowest, lst):
    nonlocal ans
    if cnt > n:
        return
    for i in range(lowest, m+1):
        lst.append(i)
        dfs(cnt+1, i, lst)
        lst.pop()
    if cnt == n:
        num = 0
        for i in range(q):
            a, b, c, d = ab[i]
            if lst[b-1] - lst[a-1] == c:
                num += d
        ans = max(num, ans)

n,m,q = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0
dfs(0, 1, [])

print(ans)

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

ans = 0
for a in combinations_with_replacement(range(1,m+1), n):
  t = 0
  for i in abcd:
    if a[i[1]-1] - a[i[0]-1] == i[2]: t += i[3]
  if ans < t: ans = t
print(ans)

import itertools

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

ans=0
for A in itertools.combinations_with_replacement([m for m in range(1,M+1)], N):
  ans2=0
  for (a,b,c,d) in ABCD:
    if A[b-1]-A[a-1]==c:
      ans2+=d
  ans=max(ans, ans2)
print(ans)


import itertools
n, m, q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]
l = list(itertools.combinations_with_replacement(range(1, m+1), n))
ans = 0
for i in l:
    w = 0
    for j in abcd:
        if i[j[1]-1] - i[j[0]-1] == j[2]:
            w += j[3]
    ans = max(ans, w)
print(ans)

import itertools

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

L = list(itertools.combinations_with_replacement(range(1,M+1), N))
p_list =  []

for l in L :
    point = 0
    for x in X :
        if l[x[1]-1] - l[x[0]-1] == x[2] :
            point += x[3]
            
    p_list.append(point)

# print(X)
# print(L)
# print(p_list)
print(max(p_list))

n,m,q = map(int,input().split())
A = []
B = []
C = []
D = []
for i in range(q):
  a,b,c,d = map(int,input().split())
  A.append(a-1)
  B.append(b-1)
  C.append(c)
  D.append(d)
  
from collections import deque
que = deque([[1]])
M = 0

while que:
  
  lst = que.popleft()
  if len(lst) == n:
    s = 0
    for i in range(q):
      if lst[B[i]] - lst[A[i]] == C[i]:
        s += D[i]
    if M < s:
      M = s
      
  else:    
    for j in range(lst[-1],m+1):
      lst2 = lst +[j]
      que.append(lst2)
    
print(M)  

#!/usr/bin/env python3
import sys
from itertools import chain

# import numpy as np
# from itertools import combinations as comb
# from bisect import bisect_left, bisect_right, insort_left, insort_right
# from collections import Counter


def solve(
    N: int, M: int, Q: int, ABCD: "List[(int,int,int,int)]",
):
    def dfs(A, l, r, n):
        if n > 0:
            max_score = 0
            for Ai in range(l, r + 1):
                A.append(Ai)
                score = dfs(A, Ai, r, n - 1)
                if score > max_score:
                    max_score = score
                A.pop()
            return max_score
        else:
            score = 0
            for a, b, c, d in ABCD:
                if A[b - 1] - A[a - 1] == c:
                    score += d
            return score

    return dfs([], 1, M, N)


def main():
    tokens = chain(*(line.split() for line in sys.stdin))
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    ABCD = []
    for i in range(Q):
        a = int(next(tokens))
        b = int(next(tokens))
        c = int(next(tokens))
        d = int(next(tokens))
        ABCD.append((a, b, c, d))
    answer = solve(N, M, Q, ABCD)
    print(answer)


def __starting_point():
    main()

__starting_point()

from itertools import combinations_with_replacement as comb_r
n,m,q=map(int,input().split())
lst=[list(map(int,input().split())) for _ in range(q)]

mx=0
for x in comb_r(range(1,m+1),n):
    sm=0
    for y in lst:
        if x[y[1]-1]-x[y[0]-1]==y[2]:
            sm+=y[3]
    if mx<sm : mx=sm

print(mx)


def f(A, N, M, Q, conditions):
    L = len(A) - 1

    if L > N:
        return

    elif L < N:
        fans = 0
        for j in range(A[L], M + 1):
            fans = max(fans ,f(A + [j], N, M, Q, conditions))

    else: # L = N
        fans = 0
        for i in range(Q):
            a, b, c, d = conditions[i]
            if A[b] - A[a] == c:
                fans += d
        
    return fans

N, M, Q = [int(x) for x in input().split()]

conditions = []
for i in range(Q):
    c = [int(x) for x in input().split()]
    conditions.append(c)

ans = f([1], N, M, Q, conditions)

print(ans)

from itertools import combinations_with_replacement

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

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

from itertools import combinations_with_replacement as cwr
[N, M, Q] = [int(i) for i in input().split()]
req = [list(map(int, input().split())) for _ in range(Q)]

ans = 0
for seq in cwr(range(1, M+1), N):
    sco = 0
    for i in range(Q):
        if seq[req[i][1]-1] - seq[req[i][0]-1] == req[i][2]:
            sco += req[i][3]
    s = max(ans, sco)
    ans = s
print(ans)

def dfs(seq):
    ans = 0
    if len(seq) == n:
        kou = 0
        for u in data:
            if seq[u[1]-1] - seq[u[0]-1] == u[2]:
                kou+=u[3]
        return kou
    else:#len(seq)==N-1からの遷都を考えると良い
        for i in range(seq[-1], m+1):
            seq_next=seq+[i]
            ans = max(ans, dfs(seq_next))
        return ans

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

score = dfs([1])
print(score)


import sys
N, M, Q = map(int, input().split())
abcd = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]
ans = 0
done = set()
todo = [(i,) for i in range(1, M + 1)]
while todo:
    A = todo.pop()
    done.add(A)
    if len(A) == N:
        total = 0
        for a, b, c, d in abcd:
            if A[b-1] - A[a-1] == c:
                total += d
        ans = max(ans, total)
        continue
    for i in range(A[-1], M + 1):
        nA = A + (i,)
        if not nA in done:
            todo.append(nA)
print(ans)

N,M,Q = map(int,input().split())
abcd = [list(map(int,input().split())) for i in range(Q)]
from itertools import combinations_with_replacement
m = [i for i in range(1,M+1)]
A = [i for i in combinations_with_replacement(m,N) if i[0] == 1]
ans = 0
for i in A:
    an = 0
    for a,b,c,d in abcd:
        if i[b-1] - i[a-1] == c:
            an += d
    ans = max(ans,an)
print(ans)

N, M, Q = map(int, input().split())
a = []
b = []
c = []
d = []
for _ in range(Q):
    ad, bd, cd, dd = map(int, input().split())
    a.append(ad-1)
    b.append(bd-1)
    c.append(cd)
    d.append(dd)
def score(A):
    tmp = 0
    for ai, bi, ci, di in zip(a, b, c, d):
        if A[bi] - A[ai] == ci:
            tmp += di
    return tmp
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([]))

n,m,q=map(int, input().split())
abcd=[]
for i in range(q):
  a,b,c,d=map(int, input().split())
  abcd.append([a,b,c,d])
def dfs(i):
  if i==0:
    alist=[]
    for k in range(m):
      alist.append([k+1])
    return alist
  elif i>0:
    blist=[]
    for k in dfs(i-1):
      last=k[-1]
      for l in range(last,m+1):
        clist=k.copy()
        clist.append(l)
        blist.append(clist)
    return blist
ans=0
for alist in dfs(n-1):
  cnt=0
  for i in range(q):
    if alist[abcd[i][1]-1]-alist[abcd[i][0]-1]==abcd[i][2]:
      cnt+=abcd[i][3]
  ans=max(ans,cnt)
print(ans)

N,M,Q = map(int,input().split())
abcd = [list(map(int,input().split())) for i in range(Q)]
from itertools import combinations_with_replacement
m = [i for i in range(1,M+1)]
A = [i for i in combinations_with_replacement(m,N) if i[0] == 1]
ans = 0
for i in A:
    an = 0
    for a,b,c,d in abcd:
        if i[b-1] - i[a-1] == c:
            an += d
    ans = max(ans,an)
print(ans)

from collections import deque

N, M, Q = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(Q)]
que = deque()
for i in range(1, M+1):
  que.append([i])
ans = 0

while que:
  check = que.popleft()
  tmp = 0
  if len(check) == N:
    for A in lst:
      if check[A[1]-1] - check[A[0]-1] == A[2]:
        tmp += A[3]
    ans = max(ans, tmp)
  else:
    for j in range(check[-1], M+1):
      que.append(check + [j])

print(ans)

import itertools
n,m,q=map(int,input().split())
req=[list(map(int,input().split())) for _ in range(q)]
a=itertools.combinations_with_replacement(range(1,m+1),n)
ans=0
for i in a:
    point=0
    for j in req:
        if i[j[1]-1]-i[j[0]-1]==j[2]:
            point+=j[3]
    ans=max(ans,point)
print(ans)

import itertools

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

def check(A, Q):
    if A[Q[1]-1] - A[Q[0]-1] == Q[2]:
        return Q[3]
    else:
        return 0

ans = 0
for i in itertools.combinations_with_replacement(list(range(1, m+1)), n):
    tmp = 0
    for j in a:
        tmp += check(i, j)
    if ans < tmp:
        ans = tmp

print(ans)


import itertools
 
n,m,q = map(int,input().split())
abcd = [list(map(int,input().split())) for _ in range(q)]
 
ans = 0
for A in itertools.combinations_with_replacement(range(1,m+1),n):
    tmp = 0
    for i in range(q):
        a,b,c,d = abcd[i]
        if A[b-1] - A[a-1] == c:
            tmp += d
    ans = max(ans,tmp)
 
print(ans)

abcd = []
max_ans = 0

def resolve(l):
  total = 0
  for a, b, c, d in abcd:
    if l[b-1] - l[a-1] == c:
      total += d

  return total

def comb(l, start, end, now, suspend):
  nonlocal max_ans
  if now == suspend:
    max_ans = max([max_ans, resolve(l)])
    return

  for i in range(start, end+1, 1):
    comb(l + [i], i, end, now+1, suspend)


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

for i in range(q):
  abcd.append(list(map(int, input().split())))

comb([], 1, m, 0, n)

print(max_ans)



import itertools

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

ans = 0
for A in itertools.combinations_with_replacement(list(range(1,m+1)),n):
    tmp = 0
    for i in range(q):
        a,b,c,d = abcd[i]
        if A[b-1] - A[a-1] == c:
            tmp += d
    ans = max(ans,tmp)

print(ans)


import itertools

N, M, Q = map(int, input().split())
a = [0] * Q
b = [0] * Q
c = [0] * Q
d = [0] * Q
for i in range(Q):
    a[i], b[i], c[i], d[i] = map(int, input().split())
    a[i] -= 1
    b[i] -= 1

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

from sys import stdin
import itertools

N, M, Q = [int(x) for x in stdin.readline().rstrip().split()]
a = [0]*Q 
b = [0]*Q
c = [0]*Q
d = [0]*Q

for i in range(Q):
  a[i], b[i], c[i], d[i] = [int(x) for x in stdin.readline().rstrip().split()]
  a[i] -= 1
  b[i] -= 1

ans = 0

#print(list(itertools.combinations( list(range(1, M+1)), N)))
for A in itertools.combinations_with_replacement( list(range(1, M+1)), N ):
  #print("A:" + str(A))  # (1, 2, 3), (1, 2, 4) ...

  cur = 0

  for i in range(Q):
    if ( A[b[i]] - A[a[i]] == c[i] ):
      #print("a:" + str(a[i]) + " / b:" + str(b[i]) + " / A[b[i]]:" + str(A[b[i]]) + " / A[a[i]]:" + str(A[a[i]]) + " / c[i]:" + str(c[i]) + " / d[i]:" + str(d[i]) + " -> Matched" )
      cur += d[i]

  #print("tmp ans for A:" + str(cur))

  if (ans < cur):
    #print("Overwrite ans")
    ans = cur

print(ans)


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

ans = 0
for a in range(M):
    for b in range(a, M):
        for c in range(b, M):
            for d in range(c, M):
                for e in range(d, M):
                    for f in range(e, M):
                        for g in range(f, M):
                            for h in range(g, M):
                                for i in range(h, M):
                                    for j in range(i, M):
                                        tmp = [a, b, c, d, e, f, g, h, i, j]
                                        tmp_ans = 0
                                        for q in range(Q):
                                                if tmp[L[q][1]-1] - tmp[L[q][0]-1] == L[q][2]:
                                                    tmp_ans += L[q][3]

                                        ans = max(ans, tmp_ans)

print(ans)

# coding: utf-8
import itertools
def main():
    n, m, q = map(int, input().split())
    matrix = []
    for _ in range(q):
        row = list(map(int, input().split()))
        matrix.append(row)
    # print(matrix)
    A = list(range(1, m+1))
    ans = 0
    for tmp_A in itertools.combinations_with_replacement(A, n):
        tmp_A = sorted(list(tmp_A))
        tmp_ans = 0
        for row in matrix:
            # print("row", row)
            # print("tmp_A", tmp_A)
            tmp = tmp_A[row[1] - 1] - tmp_A[row[0]- 1]
            if row[2] == tmp:
                tmp_ans += row[3]
        if tmp_ans >= ans:
            ans = tmp_ans
            
    print(ans)
    
main()

from itertools import combinations_with_replacement as c
N, M, Q = map(int, input().split())
l1 = list(list(map(int, input().split())) for _ in range(Q))
ans = 0

for l2 in c(range(1, M + 1), N):
    total = 0
    for i in range(Q):
        if l2[l1[i][1] - 1] - l2[l1[i][0] - 1] == l1[i][2]:
            total += l1[i][3]
    ans = max(ans, total)

print(ans)