#!/usr/bin/perl -w

use strict;

my $timestamp = <>;
my @buses = grep { /\d+/ } split /,/, <>;

my ($min_del, $min_bus);
for my $bus (@buses) {
	my $delay = $bus*(1+int(($timestamp-1)/$bus))-$timestamp;
	print "Bus $bus delay $delay\n";
	if (!defined $min_del || $min_del > $delay) {
		$min_bus = $bus;
		$min_del = $delay;
	}
}

print "$min_bus * $min_del = ", $min_bus * $min_del, "\n";

