#!/usr/bin/perl -w

use v5.36;
use strict;
use experimental 'multidimensional';

my @cubes = sort { $a->[0] <=> $b->[0] } map { [ /(\d+)/g ] } <>;
my %cubes = map { join(',', @$_) => 1 } @cubes;

my @q = ([ @{$cubes[0]}, 0, -1 ]);
my %seen;
while (@q) {
	my $face = shift @q;
	next if $seen{join(',', @$face)}++;

	for my $axis (0 .. 2) {
		next if $axis == $face->[3];
		for my $dir (-1, 1) {
			my @neigh = @{$face}[0 .. 2];
			$neigh[$axis] += $dir;
			$neigh[$face->[3]] += $face->[4];
			# say "upper? @neigh";
			if ($cubes{join(',',@neigh)}) {
				push @q, [ @neigh, $axis, -$dir ];
				# say "upper cube at @{$q[-1]}";
				next;
			}

			$neigh[$face->[3]] -= $face->[4];
			# say "side? @neigh";
			if ($cubes{join(',',@neigh)}) {
				push @q, [ @neigh, $face->[3], $face->[4] ];
				# say "side cube at @{$q[-1]}";
				next;
			}

			$neigh[$axis] -= $dir;
			push @q, [ @neigh, $axis, $dir ];
			# say "this cube at @{$q[-1]}";
		}
	}
}

say scalar keys %seen;
