#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const size_t MAX_N = (size_t) 1e5 + 5;
ll a[MAX_N], b[MAX_N];
int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
    ll res = 0;
    for (int i = n - 1; i + 1; i--) 
        res += (b[i] - (a[i] + res) % b[i]) % b[i];
    cout << res;
}