import java.util.*

fun main(args: Array<String>) {
    val sc = Scanner(System.`in`)

    val n = sc.nextInt()
    val a = Array(n) { 0L }
    val b = Array(n) { 0L }
    var ans = 0L

    for (i in 0 until n) {
        a[i] = sc.nextLong()
        b[i] = sc.nextLong()
    }

    for (i in n - 1 downTo 0) {
        a[i] += ans
        if (a[i] % b[i] != 0L) {
            ans += Math.abs(a[i] % b[i] - b[i])
        }
    }
    println(ans)
}
