#!/usr/bin/perl -w

use v5.16;
use List::Util qw(sum);

my %orb;
while (<>) {
	chomp;
	my @b = split /\)/;
	$orb{$b[0]}{$b[1]} = 1;
}

sub walk {
	my ($b, $depth) = @_;
	my @rv;
	$rv[0] = 0 if $b eq 'YOU';
	$rv[1] = 0 if $b eq 'SAN';
	say "walking $b at $depth, rv=", ($rv[0] // '_'), ',', ($rv[1] // '_');
	for my $x (keys %{ $orb{$b} }) {
		say "$b->$x";
		my ($v1, $v2) = walk($x, $depth+1);	
		say "$b<-$x, rv=", ($rv[0] // '_'), ',', ($rv[1] // '_');
		$rv[0] //= $v1;
		$rv[1] //= $v2;
		if (defined $rv[0] && defined $rv[1]) {
			say "Back at $b: $rv[0],$rv[1]";
			say $rv[0]+$rv[1] - 2;
			exit 0;
		}
	}
	$rv[0]++ if defined $rv[0];
	$rv[1]++ if defined $rv[1];
	say "returning from $b at $depth, rv=", ($rv[0] // '_'), ',', ($rv[1] // '_');
	return @rv;
}

walk('COM', 0);

		
		
		

