#include<bits/stdc++.h>
#define N 100005
using namespace std;
long long n, i, j, k, a[N], l, t, r, m, p, b[N];
int main(){
    cin>>n;
    for(i=0;i<n;i++)cin>>a[i]>>b[i];
    for(i=n-1;i>=0;i--){
        a[i]+=t;
        if(a[i]%b[i]==0)continue;
        t+=(b[i]-a[i]%b[i]);
    }
    cout<<t<<endl;
}