#include <bits/stdc++.h>
using namespace std;
int main(void){
    // Your code here!
    long long n,s=0;
    cin>>n;
    vector<long long> va(n,0);
    vector<long long> vb(n,0);
    for(int i=0;i<n;i++){
        cin>>va[i]>>vb[i];
    }
    for(int i=n-1;i>=0;i--){
        s+=((vb[i]-(va[i]+s)%vb[i])%vb[i]);
    }
    cout<<s<<endl;
}
