import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;


void main()
{
  auto n = readln.chomp.to!int;

  auto a = new long[](n);
  auto b = new long[](n);

  long res;
  foreach (i; 0..n) {
    auto x = readln.chomp.split.to!(long[]);
    a[i] = x[0];
    b[i] = x[1];
  }
  foreach_reverse(i; 0..n) {
    if (b[i] == 1) continue;
    auto t = a[i] + res;
    auto s = b[i] - (t % b[i]);
    res += s;
  }
  res.writeln;
}
