#!/usr/bin/perl -w

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

$/ = "\n\n";
my @seeds = map { $_ } scalar(<>) =~ /\d+/g;
my @ranges;
for my ($st, $ct) (@seeds) {
	push @ranges, [ $st, $ct];
}

while (<>) {
	chomp;
	my @rules = split /\n/;
	say shift @rules;
	@rules = map { [ /\d+/g ] } @rules;
	my @nranges;
RANGE:
	while (@ranges) {
		my $range = shift @ranges;
		my ($g0, $gn) = @$range;
		$gn += $g0 - 1;

		for my $rule (@rules) {
			my ($d0, $r0, $rn) = @$rule;
			$rn += $r0 - 1;
			next if $gn < $r0 || $g0 > $rn; # no overlap
			if ($g0 >= $r0 && $gn <= $rn) { # contained
				push @nranges, [ $g0+$d0-$r0, $gn-$g0+1 ];
				next RANGE;
			}
			if ($g0 < $r0 && $gn >= $r0) { # end of range overlap
				push @ranges, [ $g0, $r0-$g0 ],
					[ $r0, $gn-$r0+1 ];
				next RANGE;
			}
			if ($g0 >= $r0 && $g0 <= $rn) { # start of range ovlp
				push @ranges, [ $g0, $rn-$g0+1 ],
					[ $rn+1, $gn-$rn ];
				next RANGE;
			}
		}
		push @nranges, $range; # no match
	}
	@ranges = @nranges;
	say map { $_->[0], '+', $_->[1], ' ' } @ranges;
}

say min map { $_->[0] } @ranges;
