#include <bits/stdc++.h>
using namespace std;

typedef long long i64;
i64 a[100010], b[100010];
i64 add;

int main()
{
    int n;
    cin >> n;
    for (int i(1); i <= n; i++)
        cin >> a[i] >> b[i];

    for (int i(n); i >= 1; i--)
        add += (b[i] - (a[i] + add) % b[i]) % b[i];

    cout << add;
    return 0;
}
