#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll A[101010], B[101010];
ll n, ans;

int main()
{
	ll i;
	
	scanf("%lld", &n);
	
	for(i=0;i<n;i++){
		scanf("%lld%lld", A+i, B+i);
	}
	
	for(i--;i>=0;i--){
		ans += (B[i] - (A[i] + ans) % B[i]) % B[i];
	}
	
	printf("%lld\n", ans);
	
	return 0;
}