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

int main()
{
	ll N;
	cin >> N;
	vector<ll> A(N), B(N);
	for (int i = 0; i < N; i++) {
		cin >> A[i] >> B[i];
	}
	ll res = 0;
	for (int i = N - 1; i >= 0; i--) {
		A[i] += res;
		ll now = (B[i] - A[i] % B[i]) % B[i];
		res += now;
	}
	cout << res << endl;
	return 0;
}
