#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my ($x, $y) = split / /, <>; chomp $y; if ($x == $y) { print 0; exit; } my @q; push @q, [$y, $x, 1]; push @q, [$x + $y, $x - $y, 1]; my %vis; while (scalar @q) { my $tup = shift @q; my ($x, $y, $c) = @{$tup}; if ($x == $y) { print $c; exit; } if (!exists $vis{$tup}) { $vis{$tup} = 1; push @q, [$y, $x, $c + 1]; push @q, [$x + $y, $x - $y, $c + 1]; } } print -1