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

int main() {
  int N;
  cin >> N;
  long push=0;
  int a[N];
  int b[N];
  for(int i=0;i<N;i++)cin >> a[i] >> b[i];
  for(int i=N-1;i>-1;i--){
    if((a[i]+push)%b[i]!=0){
      int x;
      x=b[i]-(a[i]+push)%b[i];
      push+=x;
    }
  }
  cout << push;
}
