program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i,ans,push
    integer(int64), allocatable:: a(:),b(:)

    read*, n
    allocate(a(n),b(n))
    do i=1,n
        read*, a(i),b(i)
    end do

    ans=0
    do i=n,1,-1
        push = mod(b(i)-mod(a(i)+ans,b(i)),b(i))
        ans = ans + push
    end do
    print'(i0)', ans
end program name