fun main(args:Array<String>) {
    val n = readLine()!!.toInt()
    val numList = (1..n).map { readLine()!!.split(" ").map { it.toLong() }.toMutableList() }.reversed()
    var ans = 0L
    for(num in numList) {
        val md = (num[0]+ans) % num[1]
        if(md == 0L) {
            continue
        }
        ans += num[1] - md
    }
    println(ans)
}
