fun main(){
    val (n, m, q) = readLine()!!.split(" ").map { it.toInt() }
    val aArray = Array(q){ 0 }
    val bArray = Array(q){ 0 }
    val cArray = Array(q){ 0 }
    val dArray = Array(q){ 0 }
    val bigAArray = Array(n){ 0 }
    var count = 0
    var ans = 0
    for (i in 0 until q){
        val (a, b, c, d) = readLine()!!.split(" ").map { it.toInt() }
        aArray[i] = a
        bArray[i] = b
        cArray[i] = c
        dArray[i] = d
    }
    for (i in 1 until m+1) {
        bigAArray[0] = i
        for (ii in bigAArray[0] until m + 1) {
            bigAArray[1] = ii
            if (n >= 3) {
                for (iii in bigAArray[1] until m + 1) {
                    bigAArray[2] = iii
                    if (n >= 4) {
                        for (iiii in bigAArray[2] until m + 1) {
                            bigAArray[3] = iiii
                            if (n >= 5) {
                                for (iiiii in bigAArray[3] until m + 1) {
                                    bigAArray[4] = iiiii
                                    if (n >= 6) {
                                        for (iiiiii in bigAArray[4] until m + 1) {
                                            bigAArray[5] = iiiiii
                                            if (n >= 7) {
                                                for (iiiiiii in bigAArray[5] until m + 1) {
                                                    bigAArray[6] = iiiiiii
                                                    if (n >= 8) {
                                                        for (iiiiiiii in bigAArray[6] until m + 1) {
                                                            bigAArray[7] = iiiiiiii
                                                            if (n >= 9) {
                                                                for (iiiiiiiii in bigAArray[7] until m + 1) {
                                                                    bigAArray[8] = iiiiiiiii
                                                                    if (n >= 10) {
                                                                        for (iiiiiiiiii in bigAArray[8] until m + 1) {
                                                                            bigAArray[9] = iiiiiiiiii
                                                                            for (x in 0 until q){
                                                                                if (bigAArray[bArray[x]-1] - bigAArray[aArray[x]-1] == cArray[x]){
                                                                                    count += dArray[x]
                                                                                }
                                                                            }
                                                                            if (count >= ans){
                                                                                ans = count
                                                                                count = 0
                                                                            } else {
                                                                                count = 0
                                                                            }
                                                                        }
                                                                    } else {
                                                                        for (x in 0 until q){
                                                                            if (bigAArray[bArray[x]-1] - bigAArray[aArray[x]-1] == cArray[x]){
                                                                                count += dArray[x]
                                                                            }
                                                                        }
                                                                        if (count >= ans){
                                                                            ans = count
                                                                            count = 0
                                                                        } else {
                                                                            count = 0
                                                                        }
                                                                    }
                                                                }
                                                            } else {
                                                                for (x in 0 until q){
                                                                    if (bigAArray[bArray[x]-1] - bigAArray[aArray[x]-1] == cArray[x]){
                                                                        count += dArray[x]
                                                                    }
                                                                }
                                                                if (count >= ans){
                                                                    ans = count
                                                                    count = 0
                                                                } else {
                                                                    count = 0
                                                                }
                                                            }
                                                        }
                                                    } else {
                                                        for (x in 0 until q){
                                                            if (bigAArray[bArray[x]-1] - bigAArray[aArray[x]-1] == cArray[x]){
                                                                count += dArray[x]
                                                            }
                                                        }
                                                        if (count >= ans){
                                                            ans = count
                                                            count = 0
                                                        } else {
                                                            count = 0
                                                        }
                                                    }
                                                }
                                            } else {
                                                for (x in 0 until q){
                                                    if (bigAArray[bArray[x]-1] - bigAArray[aArray[x]-1] == cArray[x]){
                                                        count += dArray[x]
                                                    }
                                                }
                                                if (count >= ans){
                                                    ans = count
                                                    count = 0
                                                } else {
                                                    count = 0
                                                }
                                            }
                                        }
                                    } else {
                                        for (x in 0 until q){
                                            if (bigAArray[bArray[x]-1] - bigAArray[aArray[x]-1] == cArray[x]){
                                                count += dArray[x]
                                            }
                                        }
                                        if (count >= ans){
                                            ans = count
                                            count = 0
                                        } else {
                                            count = 0
                                        }
                                    }
                                }
                            } else {
                                for (x in 0 until q){
                                    if (bigAArray[bArray[x]-1] - bigAArray[aArray[x]-1] == cArray[x]){
                                        count += dArray[x]
                                    }
                                }
                                if (count >= ans){
                                    ans = count
                                    count = 0
                                } else {
                                    count = 0
                                }
                            }
                        }
                    } else {
                        for (x in 0 until q){
                            if (bigAArray[bArray[x]-1] - bigAArray[aArray[x]-1] == cArray[x]){
                                count += dArray[x]
                            }
                        }
                        if (count > ans){
                            ans = count
                            count = 0
                        } else {
                            count = 0
                        }
                    }
                }
            } else {
                for (x in 0 until q){
                    if (bigAArray[bArray[x]-1] - bigAArray[aArray[x]-1] == cArray[x]){
                        count += dArray[x]
                    }
                }
                if (count > ans){
                    ans = count
                    count = 0
                } else {
                    count = 0
                }
            }
        }
    }
    println(ans)
}



