// ==================================================
// > PART 1
// ==================================================
$time = int($input->lines[0]);
$buses = $input->lines[1]->numbers();
$waiting = $buses->mapAssoc(fn ($i, $b) => [$b => $b - $time % $b]);
$min_waiting = $waiting->min();
$solution_1 = $waiting->search($min_waiting) * $min_waiting;
// ==================================================
// > PART 2
// ==================================================
$buses = $input->lines[1]->split(",")->filter(fn ($b) => $b != "x")->map("int");
[$solution_2, $mult] = [1, 1];
foreach ($buses as $d => $c) {
for ($f = $solution_2; ($f + $d) % $c; $f += $mult) {}
$solution_2 = $f;
$mult *= $c;
}