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