#!/usr/bin/perl

use v5.30;
use strict;

my %map;
$; = ',';
my $y = 0;
my $px;
while (<>) {
	my $x = 0;
	chomp;
	for (split //) {
		$map{$x,$y} = $_ eq '#' ? 1 : 0;
		$x++;
	}
	$px = ($x-1)/2;
	$y++;
}
my $py = ($y-1)/2;
my $dir = 0; # up right down left

my $sum;

for (1 .. 10_000) {
	my $state = $map{$px,$py};
	if ($state) {
		$dir = 0 if ++$dir > 3;
	} else {	
		$dir = 3 if --$dir < 0;
		$sum++;
	}
	$map{$px,$py} = !$state;
	$py-- if $dir == 0;
	$px++ if $dir == 1;
	$py++ if $dir == 2;
	$px-- if $dir == 3;
}

say $sum;
