#include <iostream>
#include <vector>

using namespace std;

int main()
{
	int N;
	cin >> N;
	vector<long long> 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) {
		auto x = A[i] + ans;
		ans += (x + B[i] - 1) / B[i] * B[i] - x;
	}
	cout << ans << endl;
}
