class Calc0141 def initialize(args) args = args.map { |l| l.chomp.split(/\s+/) } @m, @n = args.shift.map(&:to_i) end def run a = Rational(@m, @n) c = 0 while true if a.denominator == 1 return c + a.to_i - 1 elsif a > 1 c += a.floor a = a - a.floor else c += 1 a = 1 / a end end end end puts Calc0141.new(STDIN.readlines).run if __FILE__ == $0