import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

long calc(int[] a, int[] b) {
    long sum = 0;
    for (int i = cast(int)a.length - 1; i >= 0; i--) {
        long ai = sum + a[i];

/*
        if (ai > b[i]) {
            long m = ai % b[i];
            if (m > 0) {
                sum += b[i] - m;
            }
        }
        else {
            sum += b[i] - ai;
        }
        */

        long x = b[i] - ai % b[i];
        if (x < b[i])
            sum += x;
    }
    return sum;
}

void main() {
    int n = readint;
    int[] a, b;
    for (int i = 0; i < n; i++) {
        auto ab = readints;
        a ~= ab[0];
        b ~= ab[1];
    }
    writeln(calc(a, b));
}
