fun main(args: Array<String>) {
    agc009a()
}

fun agc009a() {
    val n = readLine()!!.toInt()
    val aList = LongArray(n)
    val bList = LongArray(n)
    (1..n).map { readLine()!!.split(" ").map { it.toLong() } }.forEachIndexed { index, list ->
        aList[index] = list[0]
        bList[index] = list[1]
    }

    var push = 0L
    (n - 1 downTo 0).forEach { i ->
        push += (bList[i] - ((aList[i] + push) % bList[i])) % bList[i]
    }

    println(push)
}
