#include<iostream>
#include<cstdio>
using namespace std;
const int N=1e5+10;
int n,a[N],b[N];
long long ans;
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;++i) scanf("%d%d",a+i,b+i);
	for (int i=n;i;--i){
		a[i]=(a[i]+ans)%b[i];
		if (a[i]) ans+=b[i]-a[i];
	}
	printf("%lld\n",ans);
	return 0;
}