#include <iostream>
using namespace std;

typedef long long ll;

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