m,n = gets.split.map(&:to_i) GCD = m.gcd(n) m /= GCD n /= GCD @x,@y = m,n def swap tmp = @x @x = @y @y = tmp end ans = 0 while @x != 1 || @y != 1 if @x < @y swap() ans += 1 else ans += @x / @y @x %= @y if @y == 1 ans -= 1 @x += 1 end end end p ans