#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define MOD 1000000007

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