package main import ( "fmt" ) func gcd(x, y int) int { if x < y { gcd(y, x) } if y == 0 { return x } return gcd(y, x%y) } func main() { var m, n int fmt.Scan(&m, &n) g := gcd(m, n) m /= g n /= g z := 0 for m*n > 1 { if n == 1 { z += m - 1 break } if m < n { m, n = n, m z++ } else { x := m / n m -= n * x z += x } } fmt.Println(z) }