<?php

$input = file_get_contents('input.txt');

[$earliest_timestamp, $buses] = explode("\n", $input);

$earliest_timestamp = (int) $earliest_timestamp;

$buses = array_filter(
    array_map(static function ($bus) {
        return $bus === 'x' ? null : (int) $bus;
    }, explode(',', $buses))
);

$timestamp = $earliest_timestamp;
while (true) {
    foreach ($buses as $bus) {
        if ($timestamp % $bus === 0) {
            echo ($timestamp - $earliest_timestamp) * $bus;
            exit;
        }
    }
    $timestamp++;
}