#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long int n,a[100005],b[100005],sum=0,i;
	scanf("%lld",&n);
	for(i=0;i<n;i++)
	{
		scanf("%lld %lld",&a[i],&b[i]);
	}
	sum=0;
	for(i=n-1;i>=0;i--)
	{
		a[i]+=sum;
		if(a[i]%b[i]==0)
		{
			;
		}
		else
		{
			sum+=b[i]-a[i]%b[i];
		}
	}
	printf("%lld\n",sum);
}