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

long long n, s, a[100000], b[100000];
int main(){
	cin >> n;
	for(int i = 0; i < n; i++) cin >> a[i] >> b[i];
	reverse(a, a+n);
	reverse(b, b+n);
	for(int i = 0; i < n; i++){
		a[i] += s;
		if(a[i] % b[i] == 0) continue;
		s += b[i] - (a[i] % b[i]);
	}
	cout << s;
}