#import <Foundation/Foundation.h>
typedef long long Int;
int main(){
	Int n;
	scanf("%lld",&n);
	Int *a = (Int*)malloc(sizeof(Int)*n);
	Int *b = (Int*)malloc(sizeof(Int)*n);
	for(Int i=0;i<n;i++) scanf("%lld %lld",a+i,b+i);
	Int ans=0;
	for(Int i=n-1;i>=0;i--){
		Int r = (a[i]+ans)%b[i];
		if(r>0) r = b[i]-r;
		ans += r;
	}
	printf("%lld\n",ans);
}