fun main(args : Array<String>) {
    val n = readLine()!!.toInt()
    val aa = mutableListOf<Int>()
    val bb = mutableListOf<Int>()

    repeat(n) {
        val (a, b) = readLine()!!.split(" ").map { it.toInt() }
        aa.add(a)
        bb.add(b)
    }

    var cnt = 0L

    for (i in n-1 downTo 0) {
        val a = aa[i] + cnt
        val b = bb[i]
        cnt += if (a % b == 0L) 0 else b - (a % b)
    }

    println(cnt)
}