import java.util.*

fun f(n: Int): Pair<Int, Int> {
    if (n < 10) {
        return Pair(0, 0)
    }
    val s = n.toString()

    var mn = 1e8.toInt()
    var mx = 0

    for (i in 0 until s.length - 1) {
        val t = s.substring(0, i) + (s[i].toInt() + s[i + 1].toInt() - 2 * '0'.toInt()).toString() + s.substring(i + 2)

        val r = f(t.toInt())
        mn = Math.min(mn, r.first + 1)
        mx = Math.min(mn, r.second + 1)
    }

    return Pair(mn, mx)
}

fun update(p: TreeMap<Long, Long>): Triple<TreeMap<Long, Long>, Long, Boolean> {
    var cnt = 0L
    var updated = false
    val res = TreeMap<Long, Long>()

    for (e in p) {
        val pair = Pair(e.key, e.value)
        if (pair.second > 1) {
            cnt += pair.second / 2
            val a = pair.first * 2

            res[a % 10] = (res[a % 10] ?: 0L) + pair.second / 2

            if (a >= 10) {
                res[a / 10] = (res[a / 10] ?: 0L) + pair.second / 2
            }

            if (pair.second % 2 == 1L) {
                res[pair.first] = (res[pair.first] ?: 0L) + 1
            }
            updated = true
        } else {
            val v = res.get(pair.first)
            res[pair.first] = v?.plus(pair.second) ?: pair.second
        }
    }

    return Triple(res, cnt, updated)
}

fun main(args: Array<String>) {
//    var q = true
//    for (i in 1..1000000) {
//        val r = f(i)
//        val s = f(String(i.toString().toCharArray().sortedDescending().toCharArray()).toInt())
////        println("$i ${r.first} ${r.second}")
//        q = q && r.first == r.second && r.first == s.first && r.second == s.second
//        if (r.first != r.second || r.first != s.first || r.second != s.second) {
//            println("$i $r $s")
//        }
//    }
//    println(q)

//    for (i in 1 .. 1000) {
//        val r = f(i)
//        val p = TreeMap<Long, Long>()
//        for (c in i.toString()) {
//            val a = c.toLong() - '0'.toLong()
//            p[a] = (p[a] ?: 0L) + 1
//        }
//        val res = solve(p)
//
//        if (r.first.toLong() != res) {
//            println("$i $res ${r.first}")
//        }
//    }

    val M = readLine()!!.toInt()

    var p = TreeMap<Long, Long>()

    for(i in 1 .. M) {
        val (d, c) = readLine()!!.split(" ").map{it.toLong()}
        p[d] = (p[d] ?: 0L) + c
    }

    println(solve(p))
}

fun solve(p: TreeMap<Long, Long>): Long {
    var p1 = p
    var res = 0L

    do {
        val (q, s, f) = update(p1)
        res += s
        p1 = q
    } while (f)

    val s = p1.map { if (it.value > 0) it.key.toString() else "" }.joinToString("")
    var t = String(s.toCharArray().sortedDescending().toCharArray())

    while (t.length > 1) {
        res++
        t = (t[0].toInt() + t[1].toInt() - 2 * '0'.toInt()).toString() + t.substring(2)
    }

    return Math.max(res, 1)
}
