fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val ab = (1..n).map { readLine()!!.split(" ").map { it.toLong() } }.reversed()

    var sum = 0L
    ab.map {
        val (a,b) = it
        val num = sum+a
        val inc = if(num%b==0L) 0 else b-num%b
        sum += inc
        inc
    }.sum().let { println(it) }
}