#!/usr/bin/perl -w

use strict;
use v5.30;

my @bot_has;
my @todo;
my @outp;
my @rule;
while (<>) {
	chomp;
	if (/value (\d+) goes to bot (\d+)/) {
		push @{ $bot_has[$2] }, $1;
		push @todo, $2 if @{ $bot_has[$2] } == 2;
	} elsif (/bot (\d+) gives low to (\w+) (\d+) and high to (\w+) (\d+)/) {
		$rule[$1] = [ $2, $3, $4, $5 ];
	} else {
		die $_;
	}
}

while (my $bot = shift @todo) {
	my ($lo, $hi) = sort { $a <=> $b } @{ $bot_has[$bot] };
	if ($lo == 17 && $hi == 61) {
		say "$bot";
		last;
	}
	my @r = @{ $rule[$bot] };
	if ($r[0] eq 'output') {
		$outp[$r[1]] = $lo;
	} else {
		push @{ $bot_has[$r[1]] }, $lo;
		push @todo, $r[1] if @{ $bot_has[$r[1]] } == 2;
	}
	
	if ($r[2] eq 'output') {
		$outp[$r[3]] = $hi;
	} else {
		push @{ $bot_has[$r[3]] }, $hi;
		push @todo, $r[3] if @{ $bot_has[$r[3]] } == 2;
	}
}
	

