#include<iostream>
using namespace std;
int main(){
    long n,ans=0;
    cin>>n;
    long a[n],b[n];
    for(int i=0;i<n;i++)cin>>a[i]>>b[i];
    for(int i=n-1;i>=0;i--,a[i]+=ans)ans+=(b[i]-a[i]%b[i])%b[i];
    cout<<ans<<endl;
    return 0;
}