# http://yukicoder.me/problems/11 use strict; use warnings; use 5.010; my $n = ; chomp($n); say solve($n); sub solve { my ($n) = @_; return 1 if $n == 1; my $move = 1; my $cur = 1; my $shortest = { 1 => 1, }; my $points = [1]; my $ans = -1; while (my $p = shift(@$points)) { my $step = bi_count($p); my $cnt = $shortest->{ $p }; #say " $p, step = $step"; if ($p + $step == $n) { $ans = $cnt + 1; last; } if (not $shortest->{ $p + $step } and $p + $step < $n) { $shortest->{ $p + $step } = $cnt + 1; push @$points, $p + $step; } if (not $shortest->{ $p - $step } and $p - $step > 0) { $shortest->{ $p - $step } = $cnt + 1; push @$points, $p - $step; } } return $ans; } sub bi_count { my ($num) = @_; my $cnt = 0; while ($num > 1) { if ($num % 2) { $cnt++; $num = ($num-1)/2; } else { $num = $num/2; } } return $cnt+1; }