//
//  main.swift
//  AtCorder
//
//  Created by KATSUHITO YASUMOTO on 2020/05/14.
//  Copyright © 2020 KATSUHITO YASUMOTO. All rights reserved.
//

import Foundation

let input_line = readLine()!.components(separatedBy: " ")

let N = Int(input_line[0])!
let M = Int(input_line[1])!
let Q = Int(input_line[2])!

var ans = 0

var inputSequences = [[Int]]()
for _ in 0..<Q {
    let input_line = readLine()!.components(separatedBy: " ")
    inputSequences.append(input_line.map({return Int($0)!}))
}

func dfs (_ arg: [Int]) {
    if arg.count == N+1 {
        var point = 0
        inputSequences.forEach({
            if arg[$0[1]] - arg[$0[0]] == $0[2] {
                point += $0[3]
            }
        })
        ans = max(point, ans)
        return
    }
    
    
    var increase = 0
    var last = arg.last!
    while last <= M {
        var sequence = arg
        sequence.append(sequence.last! + increase)
        dfs(sequence)
        increase += 1
        last += 1
    }
}
dfs([1])
print(ans)
