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

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

