@file:Suppress("NOTHING_TO_INLINE", "EXPERIMENTAL_FEATURE_WARNING", "OVERRIDE_BY_INLINE")

import java.io.PrintWriter
import java.util.PriorityQueue
import kotlin.math.*
import kotlin.random.Random
import kotlin.collections.sort as _sort
import kotlin.collections.sortDescending as _sortDescending
import kotlin.io.println as iprintln

/** @author Spheniscine */
fun main() { _writer.solve(); _writer.flush() }
fun PrintWriter.solve() {
    val numCases = 1 //readInt()
    case@ for(case in 1..numCases) {
        //print("Case #$case: ")

        val n = readInt()
        val m = readInt()
        val q = readInt()

        val B = Array(n) { mutableListOf<Req>() }

        repeat(q) {
            val a = readInt()-1
            val b = readInt()-1
            val c = readInt()
            val d = readInt()

            B[b].add(Req(a, b, c, d))
        }

        var D = HashMap<IntArrayHash, Int>()
        D[IntArrayHash(intArrayOf(1))] = 0

        for(i in 1 until n) {
            val Di = HashMap<IntArrayHash, Int>()

            for((path, score) in D) {
                Di[path + path.intArray.last()] = score
            }

            for((a, b, c, d) in B[i]) {
                for((path, score) in D) {
                    val nx = path.intArray[a] + c
                    if(nx in path.intArray.last() .. m) {
                        val np = path + nx
                        Di[np] = Di.getOrDefault(np, score) + d
                    }
                }
            }

            D = Di
        }

        val ans = D.values.max()!!

        println(ans)
    }
}

data class DPEntry(val path: IntArray, val score: Int)
operator fun IntArray.plus(v: Int) = IntArray(size + 1).also {
    for(i in indices) it[i] = this[i]
    it[size] = v
}

data class Req(val a: Int, val b: Int, val c: Int, val d: Int)
fun <K> MutableMap<K, Int>.setMax(k: K, v: Int) =
    if(get(k).let { it == null || v > it }) { put(k, v); true } else false

class IntArrayHash(val intArray: IntArray) {
    val hash = sipHasher.hash(intArray)
    override fun hashCode() = hash
    override fun equals(other: Any?) = other is IntArrayHash && intArray contentEquals other.intArray
    override fun toString() = intArray.contentToString()
}

operator fun IntArrayHash.plus(v: Int) = IntArrayHash(intArray + v)
/** HalfSipHash implementation */
private inline infix fun Int.rol(dist: Int) = shl(dist) or ushr(-dist)
val sipHasher = HalfSipHash()
class HalfSipHash(val k0: Int = random.nextInt(), val k1: Int = random.nextInt()) {
    private var v0 = 0
    private var v1 = 0
    private var v2 = 0
    private var v3 = 0

    fun init() {
        v0 = k0; v1 = k1; v2 = 0x6c796765 xor k0; v3 = 0x74656462 xor k1
    }

    private fun round() {
        v0 += v1; v1 = v1 rol 5; v1 = v1 xor v0; v0 = v0 rol 16; v2 += v3; v3 = v3 rol 8; v3 = v3 xor v2
        v0 += v3; v3 = v3 rol 7; v3 = v3 xor v0; v2 += v1; v1 = v1 rol 13; v1 = v1 xor v2; v2 = v2 rol 16
    }

    fun acc(m: Int) {
        v3 = v3 xor m
        round()
        v0 = v0 xor m
    }

    fun acc(m: Long) {
        acc(m.toInt())
        acc(m.shr(32).toInt())
    }

    private inline fun ByteArray.getOrFF(i: Int) = if(i < size) get(i).toInt() and 0xff else 0xff

    fun acc(input: String) {
        val bytes = input.toByteArray()
        val len = bytes.size
        for (i in 0 until len step 4) {
            val int = (bytes[i].toInt() shl 24
                    or (bytes.getOrFF(i+1) shl 16)
                    or (bytes.getOrFF(i+2) shl 8)
                    or bytes.getOrFF(i+3))
            acc(int)
        }
        comma()
    }

    fun comma() {
        v1 = v1 xor 0xff
        round()
    }

    fun finish(): Int {
        v2 = v2 xor 0xee
        round(); round(); round()
        return v1 xor v3
    }

    fun finishLong(): Long {
        v2 = v2 xor 0xee
        round(); round(); round()
        val h = v1 xor v3
        v1 = v1 xor 0xdd
        round(); round(); round()
        return h.toLong().shl(32) or (v1 xor v3).toLong().and(0xffff_ffff)
    }

    inline fun doHash(block: HalfSipHash.() -> Unit): Int {
        init()
        block()
        return finish()
    }

    fun hash(input: IntArray): Int {
        init()
        for (m in input) acc(m)
        comma()
        return finish()
    }

    fun hash(input: LongArray): Int {
        init()
        for (m in input) acc(m)
        comma()
        return finish()
    }

    fun hash(input: String): Int {
        init()
        acc(input)
        return finish()
    }
}


/** IO */
//@JvmField val INPUT = File("input.txt").inputStream()
//@JvmField val OUTPUT = File("output.txt").outputStream()
@JvmField val INPUT = System.`in`
@JvmField val OUTPUT = System.out

@JvmField val _reader = INPUT.bufferedReader()
fun readLine(): String? = _reader.readLine()
fun readLn() = _reader.readLine()!!
@JvmField var _ln = ""
@JvmField var _lnPtr = 0
fun read(): String {
    while (_lnPtr >= _ln.length) {
        _ln = readLine() ?: return ""
        _lnPtr = 0
    }
    var j = _ln.indexOf(' ', _lnPtr)
    if(j < 0) j = _ln.length
    val res = _ln.substring(_lnPtr, j)
    _lnPtr = j + 1
    return res
}
fun readRem(): String /* reads remainder of current line */ =
    _ln.substring(min(_lnPtr, _ln.length)).also { _lnPtr = _ln.length }
fun readInt() = read().toInt()
fun readDouble() = read().toDouble()
fun readLong() = read().toLong()
fun readStrings(n: Int) = List(n) { read() }
fun readLines(n: Int) = List(n) { readLn() }
fun readInts(n: Int) = List(n) { read().toInt() }
fun readIntArray(n: Int) = IntArray(n) { read().toInt() }
fun readDoubles(n: Int) = List(n) { read().toDouble() }
fun readDoubleArray(n: Int) = DoubleArray(n) { read().toDouble() }
fun readLongs(n: Int) = List(n) { read().toLong() }
fun readLongArray(n: Int) = LongArray(n) { read().toLong() }

@JvmField val _writer = PrintWriter(OUTPUT, false)

/** shuffles and sort overrides to avoid quicksort attacks */
private inline fun <T> _shuffle(rnd: Random, get: (Int) -> T, set: (Int, T) -> Unit, size: Int) {
    // Fisher-Yates shuffle algorithm
    for (i in size - 1 downTo 1) {
        val j = rnd.nextInt(i + 1)
        val temp = get(i)
        set(i, get(j))
        set(j, temp)
    }
}

@JvmField var _random: Random? = null
val random get() = _random ?: Random(0x594E215C123 * System.nanoTime()).also { _random = it }

fun IntArray.shuffle(rnd: Random = random) = _shuffle(rnd, ::get, ::set, size)
fun IntArray.sort() { shuffle(); _sort() }
fun IntArray.sortDescending() { shuffle(); _sortDescending() }

fun LongArray.shuffle(rnd: Random = random) = _shuffle(rnd, ::get, ::set, size)
fun LongArray.sort() { shuffle(); _sort() }
fun LongArray.sortDescending() { shuffle(); _sortDescending() }

fun DoubleArray.shuffle(rnd: Random = random) = _shuffle(rnd, ::get, ::set, size)
fun DoubleArray.sort() { shuffle(); _sort() }
fun DoubleArray.sortDescending() { shuffle(); _sortDescending() }

fun CharArray.shuffle(rnd: Random = random) = _shuffle(rnd, ::get, ::set, size)
inline fun CharArray.sort() { _sort() }
inline fun CharArray.sortDescending() { _sortDescending() }

inline fun <T: Comparable<T>> Array<out T>.sort() = _sort()
inline fun <T: Comparable<T>> Array<out T>.sortDescending() = _sortDescending()
inline fun <T: Comparable<T>> MutableList<out T>.sort() = _sort()
inline fun <T: Comparable<T>> MutableList<out T>.sortDescending() = _sortDescending()

fun `please stop removing these imports IntelliJ`() { iprintln(max(1, 2)) }

/** additional commons */
inline fun <T> Iterable<T>.sumByLong(func: (T) -> Long) = fold(0L) { acc, t -> acc + func(t) }
inline fun <T> Sequence<T>.sumByLong(func: (T) -> Long) = fold(0L) { acc, t -> acc + func(t) }
inline fun <T> Array<T>.sumByLong(func: (T) -> Long) = fold(0L) { acc, t -> acc + func(t) }
fun IntArray.sumLong() = fold(0L, Long::plus)


Measured memory_bytes: 43740