#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int NICO = 100000 + 10;
typedef long long Long;
Long a[NICO], b[NICO], n;
int main()
{
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i] >> b[i];
    }
    Long tmp = 0;
    for(int i=n;i>=1;i--)
    {
        a[i] += tmp;
        tmp += (b[i] - a[i]%b[i]) % b[i];
    }
    cout << tmp << endl;
}