#include <bits/stdc++.h>
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 c = 0, r = 0;
	for (int i = N-1; i >= 0; -- i) {
		auto x = (A[i] + c + B[i]-1) / B[i] * B[i];
		auto y = x - (A[i] + c);
		c += y;
		r += y;
	}
	cout << r << endl;
}
