#!/usr/bin/perl -w

use strict;

my $timestamp = <>;
my @buses = split /,/, <>;

my $mins = 1;
my %bus_time;
my $first = shift @buses;
for my $bus (@buses) {
	if ($bus =~ /\d/) {
		$bus_time{$bus} = $mins % $bus;
		print "Bus $bus at t+$mins ($bus_time{$bus})\n";
	}
	$mins++;
}

@buses = grep /\d/, @buses;

my $t = $first;
my $add = $first;

for my $bus (keys %bus_time) {
	print "bus $bus at $bus_time{$bus}\n";
	while (1) {
		print "t=$t, add=$add\n";
		last if ($t + $bus_time{$bus}) % $bus == 0;
		$t += $add;
	}
	$add *= $bus;
}
print "t=$t\n";

