import java.lang.Math.max
import java.lang.Math.min

fun main(args:Array<String>?): Unit {
    val n = readLine()!!.trim().toInt()
    val days = IntArray(360){0}
    repeat(n){
        val (m, d, v, s) = readLine()!!.trim().split(' ').map(String::toInt)
        val from = (m - 1) * 30 + d - 1
        val to = (from + v - 1) % 360
        for (diff in 0 until v){
            if (days[(from + diff) % 360] < s) days[(from + diff) % 360] = s
        }
        for (diff in v until 360){
            if (days[(from + diff) % 360] < max(0, s - min((from - (from + diff) + 360) % 360, (from + diff - to + 360) % 360))) {
                days[(from + diff) % 360] = max(0, s - min((from - (from + diff) + 360) % 360, (from + diff - to + 360) % 360))
            }
        }
    }
    println(days.min()!!)
}
