program AGC009A
    implicit none
    integer(8)::i,N,ans
    integer(8),allocatable,dimension(:)::A,B
    read(5,*)N
    allocate(A(N))
    allocate(B(N))
    !末尾から挿入
    read(5,*)(A(N-i),B(N-i),i=0,N-1)
    ans=0

    do i=1,N
        if(i==1)then
            ans=mod(B(i)-mod(A(i),B(i)),B(i))
        else
            A(i)=A(i)+ans
            ans=ans+mod(B(i)-mod(A(i),B(i)),B(i))
        end if
    end do

    print'(i0)',ans
end program AGC009A