#!/usr/bin/perl -w

use v5.38;
use List::Util qw(min);

$/ = "\n\n";
my @seeds = scalar(<>) =~ /\d+/g;
say join ",", @seeds;

while (<>) {
	chomp;
	my @rules = split /\n/;
	say shift @rules;
	@rules = map { [ /\d+/g ] } @rules;
	for (@seeds) {
		for my $rule (@rules) {
			if ($_ >= $rule->[1] && $_ < $rule->[1] + $rule->[2]) {
				$_ = $rule->[0] + $_ - $rule->[1];
				last;
			} 
		}
	}
	say join ",", @seeds;
}

say min @seeds;
