#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll N=1e5+5;
ll n,ans,a[N],b[N];
int main(){
    ios_base:: sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin>>n;
    for(ll i=0;i<n;i++)cin>>a[i]>>b[i];
    for(ll i=n-1;i>=0;i--)ans+=(b[i]-(a[i]+ans)%b[i])%b[i];
    cout<<ans;
}

