#include <iostream>
using namespace std;
typedef long long ll;

int main()
{
    int n;
    cin >> n;
    ll a[100002], b[100002];
    ll sum = 0;
    for(int i = 0; i < n; i++) cin >> a[i] >> b[i];
    for(int i = n - 1; i >= 0; i--){
        sum += (b[i] - (a[i] + sum) % b[i]) % b[i];
    }
    cout << sum << endl;
}