let N = Int(readLine()!)!
var ABs = [(Int, Int)]()
for _ in 0..<N {
    let AB = readLine()!.characters.split(" ").map{Int(String($0))!}
    ABs += [(AB[0], AB[1])]
}
var ans = 0
for (A, B) in ABs.reverse() {
    ans += (B-(A+ans)%B)%B
}
print(ans)