fun main(args: Array<String>?): Unit {
    val target = readLine()!!.trim().toInt()
    val filter = IntArray(14){0}
    repeat(readLine()!!.trim().toInt()) {
        val (p, q) = readLine()!!.trim().split( ' ').map(String::toInt)
        filter[p] = filter[p] or (1 shl q)
    }
    println(solve(target, filter))
}
fun solve(target: Int, filter: IntArray): Long {
    val hoursMemo = LongArray(target + 1){0}
    for (h in 0 until 24) for (m in 0 until 60) for (s in 0 until 60) {
        val t = Segment(h / 10).blighting(filter[8]) +
                Segment(h % 10).blighting(filter[9]) +
                Segment(m / 10).blighting(filter[10]) +
                Segment(m % 10).blighting(filter[11]) +
                Segment(s / 10).blighting(filter[12]) +
                Segment(s % 10).blighting(filter[13])
        if (t in 0 .. target) hoursMemo[t]++
    }
    val daysMemo = Array(31){LongArray(target + 1){0}}
    for (i in 1 .. 31) {
        val t = Segment(i / 10).blighting(filter[6]) +
                Segment(i % 10).blighting(filter[7])
        for (j in 0 .. target - t) {
            daysMemo[i - 1][j + t] = hoursMemo[j]
        }
    }
    val leapYearMemo = LongArray(target + 1){0}
    for (month in Year.Leap.months()) {
        val t = Segment(month.value / 10).blighting(filter[4]) +
                Segment(month.value % 10).blighting(filter[5])
        for (j in month.days()) for (k in 0 .. target - t) {
            leapYearMemo[k + t] += daysMemo[j - 1][k]
        }
    }
    val yearMemo = LongArray(target + 1){0}
    for (month in Year.Normal.months()){
        val t = Segment(month.value / 10).blighting(filter[4]) +
                Segment(month.value % 10).blighting(filter[5])
        for (j in month.days()) for (k in 0 .. target - t){
            yearMemo[k + t] += daysMemo[j - 1][k]
        }
    }
    val memo = LongArray(target + 1){0}
    for (year in 0 until 10000) {
        val t = Segment(year / 1000).blighting(filter[0]) +
                Segment(year / 100 % 10).blighting(filter[1]) +
                Segment(year / 10 % 10).blighting(filter[2]) +
                Segment(year % 10).blighting(filter[3])
        if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
            for (j in 0 .. target - t) memo[j + t] += leapYearMemo[j]
        }else {
            for (j in 0 .. target - t) memo[j + t] += yearMemo[j]
        }
    }
    return memo.last()
}

sealed class Year(private val months: List<Month>){
    fun months(): Iterable<Month> = months
    object Leap: Year(listOf(
            Month(1, 31),
            Month(2, 29),
            Month(3, 31),
            Month(4, 30),
            Month(5, 31),
            Month(6, 30),
            Month(7, 31),
            Month(8, 31),
            Month(9, 30),
            Month(10, 31),
            Month(11, 30),
            Month(12, 31)))
    object Normal: Year(listOf(
            Month(1, 31),
            Month(2, 28),
            Month(3, 31),
            Month(4, 30),
            Month(5, 31),
            Month(6, 30),
            Month(7, 31),
            Month(8, 31),
            Month(9, 30),
            Month(10, 31),
            Month(11, 30),
            Month(12, 31)))
}
class Month(val value: Int, private val last: Int) {
    fun days(): Iterable<Int> = 1 .. last
}
class Segment(val value: Int) {
    fun blighting(filter: Int = 0): Int {
        return filteredBlightingSegment[blightingSegment[value] and filter.inv()]
    }
    companion object {
        private val filteredBlightingSegment = IntArray(1 shl 7){it.toString(2).count{ c -> c == '1'}}
        private val blightingSegment = intArrayOf(119, 36, 93, 109, 46, 107, 123, 37, 127, 111)
    }
}


Measured memory_bytes: 44536