class Yukicoder def initialize m, n = gets.chomp.split.map(&:to_i) count = 0 cur = Rational(m,n) while cur != 1.0 m = cur.numerator # 分子 n = cur.denominator # 分母 if n == 1 count += m-1 break elsif cur > 1.0 x = m / n cur = Rational(m - n*x, n) count += x else cur = Rational(n, m) count += 1 end end puts count end end Yukicoder.new