#include <bits/stdc++.h>
using namespace std;
int n;
long long a[100009],b[100009];
int main(){
    cin>>n;
    for(int i=0;i<n;i++)cin>>a[i]>>b[i];
    long long num = 0;
    for(int i=n-1;i>=0;i--){
        a[i] += num;
        if(a[i]%b[i]!=0)
        num += (b[i] - a[i]%b[i]);
    }
    cout<<num<<endl;
}
