import std.stdio, std.string, std.conv;
import std.algorithm, std.array;
auto solve(string s_) {
    immutable N = s_.to!int();
    auto ab = new long[2][N];
    foreach(ref v;ab) v = readln.split.map!(to!long).array();
    long c=0;
    foreach_reverse(ref v;ab) {
        immutable a=v[0], b=v[1];
        c+=(b-(a+c)%b)%b;
    }
    return c;
}
void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }