#include<bits/stdc++.h>
using namespace std;
const int N(111111);
long long a[N], b[N];
int main() {
	int n;
	cin >> n;
	for(int i(1); i <= n; i++) {
		cin >> a[i] >> b[i];
	}
	long long inc(0);
	for(int i(n); i >= 1; i--) {
		a[i] += inc;
		inc += (b[i] - a[i] % b[i]) % b[i];
	}
	cout << inc << endl;
}

