#include<bits/stdc++.h>
using namespace std;
int main(){
  long N;cin>>N;
  vector<long>A(N),B(N);
  for(long i=0;i<N;i++)cin>>A[i]>>B[i];
  long c=0;
  for(long i=N-1;i>=0;i--){
    if((A[i]+c)%B[i]!=0){
      c+=B[i]-(A[i]+c)%B[i];
    }
  }
  cout<<c;
}