#include<bits/stdc++.h>
using namespace std;
using ll=long long;

int main(){
  int i,j;ll cnt=0;
  int n;
  cin>>n;
  ll a[n],b[n];
  for(i=0;i<n;i++)cin>>a[i]>>b[i];
  ll S=a[n-1];
  for(i=n-1;i>=0;i--){
    if(S%b[i]!=0){
      cnt+=b[i]-S%b[i];
      S+=b[i]-S%b[i];
    }
    if(i==0)break;
    S+=a[i-1]-a[i];
  }
  cout<<cnt;
}